Put file To Zip File : Zip Unzip « File « Java Tutorial






/*
 * Copyright 2004 Outerthought bvba and Schaubroeck nv
 *
 * 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.
 */
// revised from outerj daisy

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

public class ZipHelper {
    private static int BUFFER_SIZE = 32768;

    public static void fileToZip(File file, File zipFile, int compressionLevel) throws Exception {
        zipFile.createNewFile();
        FileOutputStream fout = new FileOutputStream(zipFile);
        ZipOutputStream zout = null;
        try {
            zout = new ZipOutputStream(new BufferedOutputStream(fout));
            zout.setLevel(compressionLevel);
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                for (int i = 0; i < files.length; i++)
                    fileToZip(files[i], zout, file);
            } else if (file.isFile()) {
                fileToZip(file, zout, file.getParentFile());
            }
        } finally {
            if (zout != null)
                zout.close();
        }
    }

    private static void fileToZip(File file, ZipOutputStream zout, File baseDir) throws Exception {
        String entryName = file.getPath().substring(baseDir.getPath().length() + 1);
        if (File.separatorChar != '/')
          entryName = entryName.replace(File.separator, "/");
        if (file.isDirectory()) {
            zout.putNextEntry(new ZipEntry(entryName + "/"));
            zout.closeEntry();
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++)
                fileToZip(files[i], zout, baseDir);
        } else {
            FileInputStream is = null;
            try {
                is = new FileInputStream(file);
                zout.putNextEntry(new ZipEntry(entryName));
                streamCopy(is, zout);
            } finally {
                zout.closeEntry();
                if (is != null)
                    is.close();
            }
        }
    }

    private static void streamCopy(InputStream is, OutputStream os) throws IOException {
        byte[] buffer = new byte[BUFFER_SIZE];
        int len;
        while ((len = is.read(buffer)) > 0) {
            os.write(buffer, 0, len);
        }
    }

}








11.59.Zip Unzip
11.59.1.Uses Zip compression to compress any number of files given on the command line
11.59.2.Decompress a zip file using ZipFile
11.59.3.Unzip
11.59.4.gzip
11.59.5.Compress a Byte Array
11.59.6.Decompress a Byte Array
11.59.7.Compress Java objects
11.59.8.Decompress Java objects
11.59.9.Compress string(byte array) by Deflater
11.59.10.Compare two zip files
11.59.11.Put file To Zip File
11.59.12.Unpack a segment from a zip
11.59.13.Unpack an archive from a URL
11.59.14.unzip File Into Directory
11.59.15.Zip a list of file into one zip file.
11.59.16.Zip up a directory
11.59.17.Zip files