Java Zip File zip(File srcFile, File destFile, String archiveRoot)

Here you can find the source of zip(File srcFile, File destFile, String archiveRoot)

Description

Zip a file or a directory.

License

Open Source License

Parameter

Parameter Description
srcFile File or directory to zip.
destFile Destination for the zipped output.

Exception

Parameter Description
IOException If IO exception occurs (messing with files here...)

Declaration

public static void zip(File srcFile, File destFile, String archiveRoot) throws IOException 

Method Source Code

//package com.java2s;
/*// ww  w  .  j  av a  2 s.  c o m
 * Copyright (C) ${year} Omry Yadan <${email}>
 * All rights reserved.
 *
 * See https://github.com/omry/banana/blob/master/BSD-LICENSE for licensing information
 */

import java.io.*;

import java.util.*;

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

public class Main {
    /**
     * Zip a file or a directory. Create a zip from the file represented by
     * <code>srcFile</code>, or all the files found under it (if a directory).
     * Output is saved to the file represented by <code>destFile</code>. Zip is
     * created compressed.
     *
     * @param srcFile File or directory to zip.
     * @param destFile Destination for the zipped output.
     * @throws IOException If IO exception occurs (messing with files here...)
     */
    public static void zip(File srcFile, File destFile, String archiveRoot) throws IOException {
        FileOutputStream fout = new FileOutputStream(destFile);
        try {
            zip(srcFile, fout, archiveRoot);
        } finally {
            fout.close();
        }
    }

    public static void zip(File srcFile, OutputStream out, String archiveRoot) throws IOException {
        Vector<File> filesVector = getFilesList(srcFile);
        if (filesVector.size() > 0) {
            String baseDirPath = null;
            if (srcFile.isDirectory()) {
                baseDirPath = srcFile.getPath();
            } else {
                baseDirPath = srcFile.getParent();
            }

            ZipOutputStream zipOut = new ZipOutputStream(out);
            try {
                File[] files = new File[filesVector.size()];
                ZipEntry[] entries = new ZipEntry[filesVector.size()];

                for (int i = 0; i < files.length; i++) {
                    files[i] = filesVector.elementAt(i);
                    String entryPath = archiveRoot + File.separatorChar
                            + files[i].getPath().substring(baseDirPath.length() + 1);
                    entries[i] = new ZipEntry(entryPath);
                    zipOut.putNextEntry(entries[i]);
                    entries[i].setTime(files[i].lastModified());
                    if (!files[i].isDirectory()) {
                        FileInputStream fileIn = new FileInputStream(files[i]);
                        try {
                            pump(fileIn, zipOut);
                        } finally {
                            fileIn.close();
                        }
                    }
                    zipOut.closeEntry();
                }
            } finally {
                zipOut.finish();
                zipOut.close();
            }
        }
    }

    /**
     * Lists all the files in a given directory (recursively).
     *
     * @param baseDir Base directory for which list is created
     * @return Vector of File objects representing all the files in baseDir.
     */
    private static Vector<File> getFilesList(File baseDir) {
        Vector<File> filesVector = new Vector<File>();
        String[] filesList = null;

        if (baseDir.isDirectory()) {
            filesList = baseDir.list();
        } else {
            filesList = new String[] { "" };
        }

        for (int i = 0; i < filesList.length; i++) {
            File file = new File(baseDir, filesList[i]);
            if (file.isDirectory()) {
                Vector<File> innerFilesVector = getFilesList(file);
                Enumeration<File> enum1 = innerFilesVector.elements();
                while (enum1.hasMoreElements()) {
                    filesVector.addElement(enum1.nextElement());
                }
            } else {
                filesVector.addElement(file);
            }
        }

        return filesVector;
    }

    /**
     * Writes the bytes read from the given input stream into the given output
     * stream until the end of the input stream is reached. Returns the amount of
     * bytes actually read/written.
     */
    public static int pump(InputStream in, OutputStream out) throws IOException {
        byte[] buf = new byte[4096];
        int count;
        int amountRead = 0;

        while ((count = in.read(buf)) != -1) {
            out.write(buf, 0, count);
            amountRead += count;
        }

        return amountRead;
    }
}

Related

  1. zip(File source, File target)
  2. zip(File sourceDir, OutputStream targetStream)
  3. zip(File src, File target)
  4. zip(File srcDir, File zipFile)
  5. zip(File srcDirectory, File destFile)
  6. zip(File theFileToZip)
  7. zip(File toZip, File outFile)
  8. zip(File[] sourceFiles, File destFile, boolean deleteOriginalFiles)
  9. zip(final File dir, final File target)