Java Zip Folder doZip(Map zipStructure, File zipFile)

Here you can find the source of doZip(Map zipStructure, File zipFile)

Description

Zip up a bunch of files.

License

Open Source License

Exception

Parameter Description
IOException an exception
Exception an exception

Declaration

private static long doZip(Map<String, File> zipStructure, File zipFile)
        throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Map;
import java.util.TreeMap;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
    /**//from ww  w  .  j a  v  a 2s  .co  m
     * Zip up a bunch of files.
     * 
     * @throws IOException
     * @throws Exception
     */
    private static long doZip(Map<String, File> zipStructure, File zipFile)
            throws IOException {
        // sort the files by key
        TreeMap<String, File> sortedStructure = new TreeMap<String, File>();
        sortedStructure.putAll(zipStructure);

        // Create the ZIP file
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
                zipFile));
        out.setComment("created with my program");
        out.setMethod(ZipOutputStream.DEFLATED);// DEFLATED or STORED
        out.setLevel(Deflater.DEFAULT_COMPRESSION);// Deflater.NO_COMPRESSION

        // Create a buffer for reading the files
        byte[] buf = new byte[1024 * 10];

        // Compress the files
        for (String key : sortedStructure.keySet()) {
            FileInputStream in = new FileInputStream(
                    sortedStructure.get(key));

            // Add ZIP entry to output stream.
            out.putNextEntry(new ZipEntry(key));

            // Transfer bytes from the file to the ZIP file
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }

            // Complete the entry
            out.closeEntry();
            in.close();
        }

        // Complete the ZIP file
        out.close();

        return zipFile.length();
    }
}

Related

  1. addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
  2. addFolderToZip(String path, String srcFolder, ZipOutputStream zip, boolean addFolder)
  3. addFolderToZip(String path, String srcFolder, ZipOutputStream zip, boolean includeFullPath)
  4. addFolderToZip(String pathInsideZip, final File folderToZip, final ZipOutputStream outZip)
  5. doZip(File fileIn, String fileOut)
  6. doZip(Properties properties, String name, File f)
  7. doZip(String baseDir, String fileName)
  8. doZip(String filename, String zipFileName)
  9. doZip(String inFilePath, String outFilePath)