Java Zip File zip(File srcDir, File zipFile)

Here you can find the source of zip(File srcDir, File zipFile)

Description

Create a zip from the specified directory.

License

Academic Free License

Declaration

public static void zip(File srcDir, File zipFile) throws IOException 

Method Source Code

//package com.java2s;
// Licensed under the Academic Free License version 3.0

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

public class Main {
    public static final File[] none = new File[0];

    /**//from w  ww .  j  a v a 2s . c  o  m
     * Create a zip from the specified directory.
     */
    public static void zip(File srcDir, File zipFile) throws IOException {
        mkdir(parent(zipFile));

        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
                new FileOutputStream(zipFile)));
        try {
            File[] kids = list(srcDir);
            for (int i = 0; kids != null && i < kids.length; ++i)
                zip(kids[i], "", out);
        } finally {
            out.close();
        }
    }

    private static void zip(File src, String parentPath, ZipOutputStream out)
            throws IOException {
        String name = src.getName();
        String path = (parentPath == "") ? name : parentPath + "/" + name;
        if (src.isDirectory()) {
            File[] kids = list(src);
            for (int i = 0; kids != null && i < kids.length; ++i)
                zip(kids[i], path, out);
        } else {
            out.putNextEntry(new ZipEntry(path));
            InputStream in = new BufferedInputStream(new FileInputStream(
                    src));
            try {
                pipe(in, out);
            } finally {
                try {
                    in.close();
                } catch (Exception e) {
                }
            }
            out.closeEntry();
        }
    }

    /**
     * Same as File.mkdirs() exception IOException is thrown on error.
     */
    public static void mkdir(File f) throws IOException {
        if (f.exists() && f.isDirectory())
            return;

        if (!f.mkdirs())
            throw new IOException("Cannot mkdir: " + f);
    }

    /**
     * Same as File.getParentFile()
     */
    public static File parent(File f) {
        return new File(f.getParent());
    }

    /**
     * Same as File.listFiles()
     */
    public static File[] list(File f) {
        String[] names = f.list();
        if (names == null || names.length == 0)
            return none;
        File[] files = new File[names.length];
        for (int i = 0; i < names.length; ++i)
            files[i] = new File(f, names[i]);
        return files;
    }

    /**
     * Pipe all the bytes available from the input stream to
     * the output stream.
     */
    public static void pipe(InputStream in, OutputStream out)
            throws IOException {
        byte[] buf = new byte[4096];
        int n;
        while ((n = in.read(buf, 0, buf.length)) >= 0)
            out.write(buf, 0, n);
    }

    /**
     * Read the specified text file into a memory char buffer.
     * The file *must* be UTF8 encoded.  This method also normalizes
     * all newlines so that \r and \r\n are replaced with \n.
     */
    public static char[] read(File f) throws IOException {
        // can't ever have more characters than bytes in file,
        // for ASCII this will be an exact size (so we don't
        // have to take a resize hit)
        char[] buf = new char[(int) f.length()];
        int n = 0;

        // open file
        InputStream in = new BufferedInputStream(new FileInputStream(f));
        try {
            // read characters
            int c, last = -1;
            while ((c = in.read()) >= 0) {
                // if not ASCII, then read rest of character
                if ((c & 0x80) != 0)
                    c = readUtf8(c, in);

                // normalize newlines and add to buffer
                if (c == '\r')
                    buf[n++] = '\n';
                else if (last == '\r' && c == '\n')
                    continue;
                else
                    buf[n++] = (char) c;
                last = c;
            }
        } finally {
            in.close();
        }

        // if we don't need to trim, then just return buf
        if (n == buf.length)
            return buf;

        // trim to size
        char[] trim = new char[n];
        System.arraycopy(buf, 0, trim, 0, n);
        return trim;
    }

    private static int readUtf8(int c0, InputStream in) throws IOException {
        // at this point 0xxx xxxx (ASCII) is already handled
        // since it is inlined into read() itself

        int c1, c2, c3;
        switch (c0 >> 4) {
        case 12:
        case 13:
            // 110x xxxx   10xx xxxx
            c1 = in.read();
            if ((c1 & 0xC0) != 0x80)
                throw new UTFDataFormatException(Integer.toHexString(c0));
            return ((c0 & 0x1F) << 6) | ((c1 & 0x3F) << 0);
        case 14:
            // 1110 xxxx  10xx xxxx  10xx xxxx
            c1 = in.read();
            c2 = in.read();
            if (((c1 & 0xC0) != 0x80) || ((c2 & 0xC0) != 0x80))
                throw new UTFDataFormatException();
            return ((c0 & 0x0F) << 12) | ((c1 & 0x3F) << 6)
                    | ((c2 & 0x3F) << 0);
        case 15:
            // 1111 0xxx  10xx xxxx  10xx xxxx  10xx xxxx
            /* I think this is valid, but Java doesn't seem to output
               characters this high - so cap things below this
            c1 = in.read();
            c2 = in.read();
            c3 = in.read();
            if (((c1 & 0xC0) != 0x80) || ((c2 & 0xC0) != 0x80) || ((c3 & 0xC0) != 0x80))
              throw new UTFDataFormatException();
            return ((c0  & 0x07) << 18) | ((c1 & 0x3F) << 12) | ((c2 & 0x3F) << 6)  | ((c3 & 0x3F) << 0);
             */
            throw new UTFDataFormatException(Integer.toHexString(c0));
        default:
            throw new UTFDataFormatException(Integer.toHexString(c0));
        }
    }
}

Related

  1. zip(File path)
  2. zip(File source, File target)
  3. zip(File source, File target)
  4. zip(File sourceDir, OutputStream targetStream)
  5. zip(File src, File target)
  6. zip(File srcDirectory, File destFile)
  7. zip(File srcFile, File destFile, String archiveRoot)
  8. zip(File theFileToZip)
  9. zip(File toZip, File outFile)