Java Zip Directory addDirToArchive(ZipOutputStream zop, File srcFile)

Here you can find the source of addDirToArchive(ZipOutputStream zop, File srcFile)

Description

Helper method that aids zipRollbackPackage in recursively creating a zip file from a directory's contents.

License

Open Source License

Parameter

Parameter Description
zop ZipOutputStream from zipRollbackPackage.
srcFile The files in this directory to be zipped.

Exception

Parameter Description
Exception an exception

Declaration

private static void addDirToArchive(ZipOutputStream zop, File srcFile)
        throws Exception 

Method Source Code

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

import java.io.*;

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

public class Main {
    /**//from  w  w  w .j av  a 2  s . co  m
     * Helper method that aids zipRollbackPackage in recursively creating a zip file from a directory's contents.
     * @param zop ZipOutputStream from zipRollbackPackage.
     * @param srcFile The files in this directory to be zipped.
     * @throws Exception
     */
    private static void addDirToArchive(ZipOutputStream zop, File srcFile)
            throws Exception {
        File[] files = srcFile.listFiles();

        for (File file : files) {
            if (file.isDirectory()) {
                addDirToArchive(zop, file);
                continue;
            }

            byte[] buffer = new byte[1024];

            FileInputStream fis = new FileInputStream(file);
            zop.putNextEntry(new ZipEntry(file.getName()));

            int length;
            while ((length = fis.read(buffer)) > 0) {
                zop.write(buffer, 0, length);
            }

            zop.closeEntry();
            fis.close();
        }
    }
}

Related

  1. addDirectoryToZip(File directory, File base, String dirPrefix, ZipOutputStream zos)
  2. addDirectoryToZip(ZipOutputStream out, File dir, String prefix)
  3. addDirectoryToZip(ZipOutputStream zipOutputStream, File dirToZip, String basePath, File fileToExclude)
  4. addDirectoryToZip(ZipOutputStream zipOutputStream, String dirName)
  5. addDirToArchive(ZipOutputStream zos, File srcFile)
  6. addDirToArchive(ZipOutputStream zos, String path, File initialDir)
  7. zip(File dir, File base, ZipOutputStream out)
  8. zip(File dir, ZipOutputStream out, String prefix)