Java Zip Directory zipDirectoryRecursive(File directory, String zipPath, ZipOutputStream zos, ArrayList avoidingFiles)

Here you can find the source of zipDirectoryRecursive(File directory, String zipPath, ZipOutputStream zos, ArrayList avoidingFiles)

Description

zip Directory Recursive

License

Open Source License

Declaration

private static void zipDirectoryRecursive(File directory, String zipPath, ZipOutputStream zos,
            ArrayList<String> avoidingFiles) throws IOException 

Method Source Code


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

import java.io.*;

import java.util.ArrayList;

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

public class Main {
    private static void zipDirectoryRecursive(File directory, String zipPath, ZipOutputStream zos,
            ArrayList<String> avoidingFiles) throws IOException {
        try {/*from w w  w  . j  av a  2  s.  c  o  m*/
            System.out.println(directory.getAbsolutePath() + "::" + zipPath);
            // get a listing of the directory content
            String[] dirList = directory.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(directory, dirList[i]);
                System.err.println("avoiding: " + avoidingFiles);
                System.err.println("file: " + f.getName());
                System.err.println("and so: " + avoidingFiles.contains(f.getName()));
                if (!avoidingFiles.contains(f.getName())) {
                    if (f.isDirectory()) {
                        // if the File object is a directory, call this
                        // function again to add its content recursively
                        // String filePath = f.getPath();
                        // File file = new File(zipPath+"/"+f.getName());
                        zipDirectoryRecursive(new File(f.getPath()), zipPath + "/" + f.getName(), zos,
                                avoidingFiles);
                        continue;
                    }
                    // 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
                    // ZipEntry anEntry = new ZipEntry(f.getPath());
                    ZipEntry anEntry = new ZipEntry(zipPath + "/" + f.getName());
                    // 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();

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void zipDirectoryRecursive(File directory, String zipPath, ZipOutputStream zos)
            throws IOException {
        try {
            System.out.println(directory.getAbsolutePath() + "::" + zipPath);
            // get a listing of the directory content
            String[] dirList = directory.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(directory, 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();
                    // File file = new File(zipPath+"/"+f.getName());
                    zipDirectoryRecursive(new File(f.getPath()), zipPath + "/" + f.getName(), zos);
                    continue;
                }
                // 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
                // ZipEntry anEntry = new ZipEntry(f.getPath());
                ZipEntry anEntry = new ZipEntry(zipPath + "/" + f.getName());
                // 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();

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related

  1. zipDirectory(String directoryName, int iBaseFolderLength, ZipOutputStream zos, CRC32 crc)
  2. zipDirectory(String directoryName, String targetName)
  3. zipDirectory(String dirName, String zipFileName)
  4. zipDirectory(ZipOutputStream out, String stripPath, File dir, char pathSeparator)
  5. zipDirectoryEntry(ZipOutputStream output, IPath entry, long time, Set directoryEntries)
  6. zipSubDirectory(String basePath, File dir, ZipOutputStream zout)