Java Zip Directory zipDirectory(String dir2zip, ZipOutputStream zos, String zipPath)

Here you can find the source of zipDirectory(String dir2zip, ZipOutputStream zos, String zipPath)

Description

zip Directory

License

Open Source License

Declaration

private static void zipDirectory(String dir2zip, ZipOutputStream zos, String zipPath)
            throws IOException, IllegalArgumentException 

Method Source Code

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

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    private static void zipDirectory(String dir2zip, ZipOutputStream zos, String zipPath)
            throws IOException, IllegalArgumentException {
        File zipDir = new File(dir2zip);
        //create a new File object based on the directory we  have to zip get a listing of the directory content 
        String[] dirList = zipDir.list();
        byte[] readBuffer = new byte[2156];
        int bytesIn = 0;
        //loop through dirList, and zip the files 
        for (int i = 0; i < dirList.length; i++) {
            File f = new File(zipDir, dirList[i]);
            if (f.isDirectory()) {
                //if the File object is a directory, call this function again to add its content recursively 
                String filePath = f.getPath();
                zipDirectory(filePath, zos, zipPath);
                continue;
            }//from w  w  w  . j  a v  a2  s.c o m
            //if we reached here, the File object f was not a directory create a FileInputStream on top of f 
            FileInputStream fis = new FileInputStream(f);
            //create a new zip entry 
            String path = f.getPath().substring(zipPath.length() + 1);
            ZipEntry anEntry = new ZipEntry(path);
            //place the zip entry in the ZipOutputStream object 
            zos.putNextEntry(anEntry);
            //now write the content of the file to the ZipOutputStream 
            while ((bytesIn = fis.read(readBuffer)) != -1) {
                zos.write(readBuffer, 0, bytesIn);
            }
            fis.close();
        }
    }

    /**
     * Zip the contents of the directory, and save it in the zipfile
     * @param dir2zip
     * @param ZipOutputStream zos
    * @throws IOException 
    * @throws IllegalArgumentException 
     * @throws IOException
     * @throws IllegalArgumentException
     */
    public static void zipDirectory(String dir2zip, ZipOutputStream zos)
            throws IllegalArgumentException, IOException {
        File zipDir = new File(dir2zip);
        if (!zipDir.isDirectory()) {
            throw new IllegalArgumentException("You must pass a directory");
        }
        String zipPath = zipDir.getPath();
        zipDirectory(dir2zip, zos, zipPath);
    }
}

Related

  1. zipDirectory(File root, File directory, ZipOutputStream zos)
  2. zipDirectory(File srcDir, File destFile)
  3. zipDirectory(File zipDir, ZipOutputStream zos)
  4. zipDirectory(final String sourceFolder, final String targetFolder, final String zipExtension)
  5. zipDirectory(String dbDumpPath)
  6. zipDirectory(String directoryName, int iBaseFolderLength, ZipOutputStream zos, CRC32 crc)
  7. zipDirectory(String directoryName, String targetName)
  8. zipDirectory(String dirName, String zipFileName)
  9. zipDirectory(ZipOutputStream out, String stripPath, File dir, char pathSeparator)