Java ZipEntry Add addZipEntry(ZipOutputStream zipOutputStream, File entryFile, String parentPath)

Here you can find the source of addZipEntry(ZipOutputStream zipOutputStream, File entryFile, String parentPath)

Description

add Zip Entry

License

Apache License

Parameter

Parameter Description
zipOutputStream a parameter
entryFile a parameter
parentPath a parameter

Exception

Parameter Description
IOException an exception

Declaration

private static void addZipEntry(ZipOutputStream zipOutputStream, File entryFile, String parentPath)
        throws IOException 

Method Source Code


//package com.java2s;
/*//from  w w  w .  j a  va 2 s . c o m
 * Copyright (c) 2007 - 2009 onozaty (http://www.enjoyxstudy.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
    /** BUF_SIZE */
    private static final int BUF_SIZE = 1024;
    /** PATH_SEPARATOR */
    private static final String PATH_SEPARATOR = "/";

    /**
     * @param zipOutputStream
     * @param entryFile
     * @param parentPath
     * @throws IOException
     */
    private static void addZipEntry(ZipOutputStream zipOutputStream, File entryFile, String parentPath)
            throws IOException {

        if (entryFile.isDirectory()) {
            String dirPath = parentPath + entryFile.getName() + PATH_SEPARATOR;

            File[] files = entryFile.listFiles();
            if (files.length == 0) {
                zipOutputStream.putNextEntry(new ZipEntry(dirPath));
            } else {
                for (int i = 0; i < files.length; i++) {
                    addZipEntry(zipOutputStream, files[i], dirPath);
                }
            }
        } else {
            InputStream entryFileStream = new FileInputStream(entryFile);

            try {
                ZipEntry entry = new ZipEntry(parentPath + entryFile.getName());

                zipOutputStream.putNextEntry(entry);

                byte[] buf = new byte[BUF_SIZE];
                int bufSize = 0;

                while ((bufSize = entryFileStream.read(buf, 0, BUF_SIZE)) != -1) {
                    zipOutputStream.write(buf, 0, bufSize);
                }

                zipOutputStream.closeEntry();
            } finally {
                entryFileStream.close();
            }
        }
    }
}

Related

  1. addZipEntry(String pathName, byte[] contents, ZipOutputStream zos)
  2. addZipEntry(ZipOutputStream zipOut, File file, String prefix)
  3. addZipEntry(ZipOutputStream zos, File fileToZip, File baseFolder)
  4. addZipEntryFromStream(InputStream is, ZipOutputStream os, String filename)
  5. addZipEntryRecursive(ZipOutputStream zipOut, File file, String prefix)