集册 Java实例教程 在ZIP的根目录添加一个条目。

在ZIP的根目录添加一个条目。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

586
在ZIP的根目录添加一个条目。

/*

 * JBoss, Home of Professional Open Source.

 * Copyright 2015, Red Hat, Inc., and individual contributors

 * as indicated by the @author tags. See the copyright.txt file in the

 * distribution for a full listing of individual contributors.

 *

 * This is free software; you can redistribute it and/or modify it

 * under the terms of the GNU Lesser General Public License as

 * published by the Free Software Foundation; either version 2.1 of

 * the License, or (at your option) any later version.

 *

 * This software is distributed in the hope that it will be useful,

 * but WITHOUT ANY WARRANTY; without even the implied warranty of

 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU

 * Lesser General Public License for more details.

 *

 * You should have received a copy of the GNU Lesser General Public

 * License along with this software; if not, write to the Free

 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA

 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.

 */

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;//时 代 J a v a - nowjava.com 提 供

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.Enumeration;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

import java.util.zip.ZipOutputStream;


public class Main{

    public static final String ENTRY_SEPARATOR = "/";

    /**

     * Adds an entry at the root of the ZIP.

     *

     * @param fileOrDir  file or directory

     * @param zos  target zip

     * @throws IOException

     *//*来自 NowJava.com*/

    public static void addToZip(File fileOrDir, ZipOutputStream zos)

            throws IOException {

        addToZip(fileOrDir, null, zos);

    }

    /**

     * Adds an entry to the ZIP at the specified path.

     * If the path is null, the entry will added at the root of the ZIP.

     *

     * @param fileOrDir  file or directory

     * @param path  path relative to the root of the ZIP

     * @param zos  target ZIP

     * @throws IOException

     */

    public static void addToZip(File fileOrDir, String path,

            ZipOutputStream zos) throws IOException {

        if (fileOrDir.isDirectory()) {

            final String dirName = path == null ? fileOrDir.getName()

                    : path + ENTRY_SEPARATOR + fileOrDir.getName();

            addDirectoryToZip(fileOrDir, dirName, zos);

        } else {

            addFileToZip(fileOrDir, path, zos);

        }

    }

    private static void addDirectoryToZip(File dir, String dirName,

            ZipOutputStream zos) throws IOException {


        final ZipEntry dirEntry = new ZipEntry(dirName + ENTRY_SEPARATOR);

        zos.putNextEntry(dirEntry);

        zos.closeEntry();


        File[] children = dir.listFiles();

        if (children != null) {

            for (File file : children) {

                if (file.isDirectory()) {

                    addDirectoryToZip(file, dirName + ENTRY_SEPARATOR

                            + file.getName(), zos);

                } else {

                    addFileToZip(file, dirName, zos);

                }

            }

        }

    }

    private static void addFileToZip(File file, String parent,

            ZipOutputStream zos) throws IOException {

        final 
展开阅读全文