compress Directory - Java java.lang

Java examples for java.lang:byte Array Compress

Description

compress Directory

Demo Code

/*// ww  w  .j  av  a2s. c o m
 * Copyright (C) 2011 Peransin Nicolas.
 * Use is subject to license terms.
 */
//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    public static void main(String[] argv) throws Exception {
        File zipTarget = new File("Main.java");
        File source = new File("Main.java");
        compressDirectory(zipTarget, source);
    }

    public static final String ZIP_FILE_SEPARATOR = "/";
    public static final int BUFFER_SIZE = 2156;
    public static final boolean DEBUG = false;

    /**
     * Target is the zip to create
     * Source is the file or directory to zip
     * 
     * @param zipTarget
     * @param source
     * @param m
     * @throws IOException
     */
    static public void compressDirectory(File zipTarget, File source)
            throws IOException {

        // create a ZipOutputStream to zip the data to.
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
                zipTarget));
        try {
            if (source.isDirectory()) {
                zipDir(source, zos, source.getName());
            } else
                zipFile(source, zos, source.getName());
        } finally {// close the stream
            zos.close();
        }
    }

    static void zipDir(File zipDir, ZipOutputStream zos, String name)
            throws IOException {
        // Create a new File object based on the directory we have to zip
        if (name.endsWith(File.separator))
            name = name.substring(0,
                    name.length() - File.separator.length());
        if (!name.endsWith(ZIP_FILE_SEPARATOR))
            name = name + ZIP_FILE_SEPARATOR;

        // Place the zip entry in the ZipOutputStream object

        // Get a listing of the directory content
        File[] dirList = zipDir.listFiles();
        if (dirList.length == 0) { // empty directory
            if (DEBUG)
                System.out.println("Add empty entry for directory : "
                        + name);
            ZipEntry anEntry = new ZipEntry(name);
            zos.putNextEntry(anEntry);
            return;
        }

        // Loop through dirList, and zip the files
        for (int i = 0; i < dirList.length; i++) {
            File f = dirList[i];
            String fName = name + f.getName();
            if (f.isDirectory()) {
                // if the File object is a directory, call this
                // function again to add its content recursively
                zipDir(f, zos, fName);
            } else {
                zipFile(f, zos, fName);
            }
        }
        return;
    }

    static void zipFile(File zipfile, ZipOutputStream zos, String name)
            throws IOException {
        // if we reached here, the File object f was not a directory 
        // create a FileInputStream on top of f

        FileInputStream fis = new FileInputStream(zipfile);
        try {
            // create a new zip entry 
            ZipEntry anEntry = new ZipEntry(name);
            if (DEBUG)
                System.out.println("Add file : " + name);
            // place the zip entry in the ZipOutputStream object
            zos.putNextEntry(anEntry);
            // now write the content of the file to the
            // ZipOutputStream
            byte[] readBuffer = new byte[BUFFER_SIZE];
            for (int bytesIn = fis.read(readBuffer); bytesIn != -1; bytesIn = fis
                    .read(readBuffer)) {
                zos.write(readBuffer, 0, bytesIn);
            }
        } finally {
            // close the Stream
            fis.close();
        }
    }
}

Related Tutorials