Java Zip Files fileToZip(File file, File zipFile)

Here you can find the source of fileToZip(File file, File zipFile)

Description

file To Zip

License

Apache License

Declaration

public static void fileToZip(File file, File zipFile) throws Exception 

Method Source Code


//package com.java2s;
/*/*from w w  w . ja  va  2 s.c  o  m*/
 *  Copyright 2013 Cloud4SOA, www.cloud4soa.eu
 *
 *  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.*;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;

public class Main {
    private static int BUFFER_SIZE = 32768;

    public static void fileToZip(File file, File zipFile) 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);
        }
    }
}

Related

  1. addToZip(String path, String srcFile, ZipOutputStream zip)
  2. addToZip(String[] sourceFiles, ZipOutputStream output)
  3. addToZip(ZipOutputStream zos, String rootDirectoryName, String fileName)
  4. addToZipFile(InputStream source, String entryName, ZipOutputStream zos)
  5. addToZipFile(String fileName, ZipOutputStream zos)
  6. fileToZipFile(File toZip, File output)
  7. makeZip(File dir, File zipFile)
  8. makeZip(String[] inFilePaths, String zipFilePath)
  9. makeZipBinary(Map instance, String fileNameSeperator)