Java Zip Directory zipDirectory(File root, File directory, ZipOutputStream zos)

Here you can find the source of zipDirectory(File root, File directory, ZipOutputStream zos)

Description

Zip directory.

License

Open Source License

Parameter

Parameter Description
root the root
directory the directory
zos the zos

Exception

Parameter Description
Exception the exception

Declaration

static void zipDirectory(File root, File directory, ZipOutputStream zos) throws Exception 

Method Source Code


//package com.java2s;
import java.io.File;
import java.io.FileInputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    /**//w  ww .j  ava2 s  . c  om
     * Zip directory.
     *
     * @param root the root
     * @param directory the directory
     * @param zos the zos
     * @throws Exception the exception
     */
    static void zipDirectory(File root, File directory, ZipOutputStream zos) throws Exception {
        for (File item : directory.listFiles()) {
            if (item.isDirectory()) {
                zipDirectory(root, item, zos);
            } else {
                byte[] readBuffer = new byte[2156];
                int bytesIn;
                FileInputStream fis = new FileInputStream(item);
                String path = item.getAbsolutePath().substring(root.getAbsolutePath().length() + 1);
                ZipEntry anEntry = new ZipEntry(path);
                zos.putNextEntry(anEntry);
                while ((bytesIn = fis.read(readBuffer)) != -1) {
                    zos.write(readBuffer, 0, bytesIn);
                }
                fis.close();
            }
        }
    }
}

Related

  1. zipDirectory(File directory, File zipFile, Pattern exclusion)
  2. zipDirectory(File directory, ZipOutputStream zipout, final FilenameFilter filter)
  3. zipDirectory(File dirToZip, File outputDir, String fileName)
  4. zipDirectory(File folder, File outputFile)
  5. zipDirectory(File inputDirectory, File zipFile)
  6. zipDirectory(File srcDir, File destFile)
  7. zipDirectory(File zipDir, ZipOutputStream zos)
  8. zipDirectory(final String sourceFolder, final String targetFolder, final String zipExtension)
  9. zipDirectory(String dbDumpPath)