Java Zip Folder zipDir(String dir2zip, ZipOutputStream zipOut, String zipFileName)

Here you can find the source of zipDir(String dir2zip, ZipOutputStream zipOut, String zipFileName)

Description

zips a directory

License

Open Source License

Parameter

Parameter Description
dir2zip a parameter
zipOut a parameter
zipFileName a parameter

Exception

Parameter Description
IOException an exception

Declaration

private static void zipDir(String dir2zip, ZipOutputStream zipOut, String zipFileName) throws IOException 

Method Source Code


//package com.java2s;
//it under the terms of the GNU Affero General Public License as published by

import java.io.BufferedInputStream;

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

import java.io.IOException;
import java.io.InputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    /**/*from  w  w  w .  j  a v a  2 s.c o m*/
     * zips a directory
     * @param dir2zip
     * @param zipOut
     * @param zipFileName
     * @throws IOException
     */
    private static void zipDir(String dir2zip, ZipOutputStream zipOut, String zipFileName) throws IOException {
        File zipDir = new File(dir2zip);
        // 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();
                zipDir(filePath, zipOut, zipFileName);
                // loop again
                continue;
            }

            if (f.getName().equals(zipFileName)) {
                continue;
            }
            // if we reached here, the File object f was not a directory
            // create a FileInputStream on top of f
            final InputStream fis = new BufferedInputStream(new FileInputStream(f));
            try {
                ZipEntry anEntry = new ZipEntry(f.getPath().substring(dir2zip.length() + 1));
                // place the zip entry in the ZipOutputStream object
                zipOut.putNextEntry(anEntry);
                // now write the content of the file to the ZipOutputStream
                while ((bytesIn = fis.read(readBuffer)) != -1) {
                    zipOut.write(readBuffer, 0, bytesIn);
                }
            } finally {
                // close the Stream
                fis.close();
            }
        }
    }
}

Related

  1. zipDir(final String dir2zip, final ZipOutputStream zos, final String root)
  2. zipDir(String baseDir, String dir2zip, ZipOutputStream zos)
  3. zipDir(String dir, ZipOutputStream zos)
  4. zipDir(String dir2zip, FilenameFilter filter, java.util.zip.ZipOutputStream zos, int bufferSize)
  5. zipDir(String dir2zip, String zipFileName)
  6. zipDir(String dir2zip, ZipOutputStream zos)
  7. zipDir(String directory, String zipName)
  8. zipDir(String origDir, File dirObj, ZipOutputStream zos)
  9. zipFolder(File root)