Description
Zip the content of the specified directory to the specified zip file.
License
Open Source License
Parameter
Parameter | Description |
---|
srcDir | The directory containing the files to zip. |
destFile | The zip file to be created. |
Exception
Declaration
public static void zipDirectory(File srcDir, File destFile) throws FileNotFoundException, IOException
Method Source Code
//package com.java2s;
/*/*from w ww . ja v a 2 s . c om*/
Copyright (C) 2007 Joao F. (joaof@sourceforge.net)
http://paccman.sourceforge.net
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Main {
private static final int BUF_SIZE = 256 * 1024;
/**
* Zip the content of the specified directory to the specified zip file.
* @param srcDir The directory containing the files to zip.
* @param destFile The zip file to be created.
* @throws java.io.FileNotFoundException
* @throws java.io.IOException
*/
public static void zipDirectory(File srcDir, File destFile) throws FileNotFoundException, IOException {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(destFile));
assert srcDir.isDirectory();
File rootDir = srcDir.getAbsoluteFile();
for (File f : srcDir.listFiles()) {
addFileToZip(zos, f, rootDir);
}
zos.close();
}
private static void addFileToZip(ZipOutputStream zos, File file, File rootDir) throws IOException {
if (file.isFile()) {
File relativeFile = new File(file.getAbsolutePath().substring(rootDir.getPath().length() + 1));
ZipEntry ze = new ZipEntry(relativeFile.getPath());
zos.putNextEntry(ze);
int count;
byte data[] = new byte[BUF_SIZE];
BufferedInputStream origin = new BufferedInputStream(new FileInputStream(file));
while ((count = origin.read(data, 0, BUF_SIZE)) != -1) {
zos.write(data, 0, count);
}
origin.close();
} else {
for (File f : file.listFiles()) {
addFileToZip(zos, f, rootDir);
}
}
}
}
Related
- zipDirectory(File directory, ZipOutputStream zipout, final FilenameFilter filter)
- zipDirectory(File dirToZip, File outputDir, String fileName)
- zipDirectory(File folder, File outputFile)
- zipDirectory(File inputDirectory, File zipFile)
- zipDirectory(File root, File directory, ZipOutputStream zos)
- zipDirectory(File zipDir, ZipOutputStream zos)
- zipDirectory(final String sourceFolder, final String targetFolder, final String zipExtension)
- zipDirectory(String dbDumpPath)
- zipDirectory(String dir2zip, ZipOutputStream zos, String zipPath)