Description
Zips up the directory contents into the specified ZipOutputStream .
License
Open Source License
Parameter
Parameter | Description |
---|
directory | The directory whose contents have to be zipped up |
zipout | The ZipOutputStream that will be populated by the files found |
filter | An optional filter that can be used to select only certain files. Can be null, in that case all files in the directory will be zipped |
Exception
Parameter | Description |
---|
IOException | an exception |
FileNotFoundException | an exception |
Declaration
public static void zipDirectory(File directory, ZipOutputStream zipout, final FilenameFilter filter)
throws IOException, FileNotFoundException
Method Source Code
//package com.java2s;
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
* (c) 2001 - 2013 OpenPlans/*w w w. j av a2 s. c o m*/
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Main {
/**
* Zips up the directory contents into the specified {@link ZipOutputStream}.
* <p>
* Note this method does not take ownership of the provided zip output stream, meaning the
* client code is responsible for calling {@link ZipOutputStream#finish() finish()} when it's
* done adding zip entries.
* </p>
*
* @param directory
* The directory whose contents have to be zipped up
* @param zipout
* The {@link ZipOutputStream} that will be populated by the files found
* @param filter
* An optional filter that can be used to select only certain files. Can be null, in
* that case all files in the directory will be zipped
* @throws IOException
* @throws FileNotFoundException
*/
public static void zipDirectory(File directory, ZipOutputStream zipout, final FilenameFilter filter)
throws IOException, FileNotFoundException {
zipDirectory(directory, "", zipout, filter);
}
/**
* See {@link #zipDirectory(File, ZipOutputStream, FilenameFilter)}, this version handles the prefix needed
* to recursively zip data preserving the relative path of each
*/
private static void zipDirectory(File directory, String prefix, ZipOutputStream zipout,
final FilenameFilter filter) throws IOException, FileNotFoundException {
File[] files = directory.listFiles(filter);
// copy file by reading 4k at a time (faster than buffered reading)
byte[] buffer = new byte[4 * 1024];
for (File file : files) {
if (file.exists()) {
if (file.isDirectory()) {
// recurse and append
String newPrefix = prefix + file.getName() + "/";
zipout.putNextEntry(new ZipEntry(newPrefix));
zipDirectory(file, newPrefix, zipout, filter);
} else {
ZipEntry entry = new ZipEntry(prefix + file.getName());
zipout.putNextEntry(entry);
InputStream in = new FileInputStream(file);
int c;
try {
while (-1 != (c = in.read(buffer))) {
zipout.write(buffer, 0, c);
}
zipout.closeEntry();
} finally {
in.close();
}
}
}
}
zipout.flush();
}
}
Related
- zipDirectory(File directory, File zip)
- zipDirectory(File directory, File zip)
- zipDirectory(File directory, File zipFile)
- zipDirectory(File directory, File zipFile)
- zipDirectory(File directory, File zipFile, Pattern exclusion)
- zipDirectory(File dirToZip, File outputDir, String fileName)
- zipDirectory(File folder, File outputFile)
- zipDirectory(File inputDirectory, File zipFile)
- zipDirectory(File root, File directory, ZipOutputStream zos)