Java Zip Folder addFolderToZip(String path, String srcFolder, ZipOutputStream zip, boolean includeFullPath)

Here you can find the source of addFolderToZip(String path, String srcFolder, ZipOutputStream zip, boolean includeFullPath)

Description

Zip the subdirectory and exclude already zipped files

License

Open Source License

Parameter

Parameter Description
path a parameter
srcFolder a parameter
zip a parameter
includeFullPath a parameter

Exception

Parameter Description

Declaration

static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip, boolean includeFullPath)
        throws IOException 

Method Source Code


//package com.java2s;
/*//from w  w w.j  a v  a  2 s.  c o  m
 * Copyright (c) 2002-2006 The European Bioinformatics Institute, and others.
 * All rights reserved. Please see the file LICENSE
 * in the root directory of this distribution.
 */

import java.io.*;

import java.util.zip.*;

public class Main {
    /**
     * Zip the subdirectory and exclude already zipped files
     * @param path
     * @param srcFolder
     * @param zip
     * @param includeFullPath
     * @throws java.io.IOException
     */
    static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip, boolean includeFullPath)
            throws IOException {
        File folder = new File(srcFolder);

        for (String fileName : folder.list()) {
            if (path.equals("") && !fileName.endsWith(".zip")) {
                if (includeFullPath) {
                    addFileToZip(folder.toString(), srcFolder + "/" + fileName, zip);
                } else {
                    addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);
                }
            } else if (!fileName.endsWith(".zip")) {
                addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip);
            }
        }
    }

    static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws IOException {

        File folder = new File(srcFile);
        if (folder.isDirectory()) {
            addFolderToZip(path, srcFile, zip, false);
        } else {
            byte[] buf = new byte[1024];
            int len;
            FileInputStream in = new FileInputStream(srcFile);
            try {
                zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
                while ((len = in.read(buf)) > 0) {
                    zip.write(buf, 0, len);
                }
            } finally {
                in.close();
            }
        }
    }
}

Related

  1. addFolderToZip(String path, File srcFolder, ZipOutputStream zip, String destZipFile)
  2. addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
  3. addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
  4. addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
  5. addFolderToZip(String path, String srcFolder, ZipOutputStream zip, boolean addFolder)
  6. addFolderToZip(String pathInsideZip, final File folderToZip, final ZipOutputStream outZip)
  7. doZip(File fileIn, String fileOut)
  8. doZip(Map zipStructure, File zipFile)
  9. doZip(Properties properties, String name, File f)