Description
Zips a directory
License
Open Source License
Parameter
Parameter | Description |
---|
srcDirectory | the directory to zip |
destFile | the newly created zip file |
Exception
Parameter | Description |
---|
Exception | an exception |
Declaration
public static void zip(File srcDirectory, File destFile) throws Exception
Method Source Code
//package com.java2s;
/*// w w w.j a v a 2s.com
* JBoss, Home of Professional Open Source.
* Copyright 2007, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Main {
/**
* Zips a directory
* @param srcDirectory the directory to zip
* @param destFile the newly created zip file
* @throws Exception
*/
public static void zip(File srcDirectory, File destFile) throws Exception {
zip(srcDirectory, destFile, false);
}
/**
* Zips a directory. If the directory is an exploded archive, then don't add the
* directory itself to the zipped file
* @param srcDirectory the directory to zip
* @param destFile the newly created zip file
* @param isArchive true if the srcDirectory is a exploded archive
* @throws Exception if an error occured during the zip
*/
public static void zip(File srcDirectory, File destFile, Boolean isArchive) throws Exception {
if (destFile.exists()) {
throw new Exception(
"The destFile [ + " + destFile + " already exists, cannot zip to an already existing file");
}
OutputStream os = new FileOutputStream(destFile);
ZipOutputStream zos = new ZipOutputStream(os);
try {
if (isArchive) {
archive("", srcDirectory, zos);
} else {
zip("", srcDirectory, zos);
}
} finally {
zos.close();
os.close();
}
}
/**
* Zips a directory
* @param prefix used to specify the parent directories for the files
* @param srcDirectory the directory to be zipped
* @param zos the ZipOutputStream to do the zipping
* @throws IOException If an exception occurs during the zip process
*/
private static void zip(String prefix, File srcDirectory, ZipOutputStream zos) throws IOException {
prefix += srcDirectory.getName() + "/";
File[] children = srcDirectory.listFiles();
for (int i = 0; i < children.length; i++) {
if (children[i].isDirectory()) {
zip(prefix, children[i], zos);
} else {
zipOneFile(prefix, children[i], zos);
}
}
}
/**
* Zips an exploded archive, this means that the directory itself is not added to the zip
* @param prefix used to specify the parent directories of the files
* @param archiveDirectory the directory to be zipped
* @param zos the ZipOutputStream used for the zip process
* @throws IOException If an exception occurs during the zip process
*/
private static void archive(String prefix, File archiveDirectory, ZipOutputStream zos) throws IOException {
File[] children = archiveDirectory.listFiles();
for (int i = 0; i < children.length; i++) {
if (children[i].isFile()) {
zipOneFile("", children[i], zos);
continue;
}
zip(prefix, children[i], zos);
}
}
/**
* Imploded an already exploded archive
* @param explodedArchive the directory of the exploded archive
* @param destArchive the name of the new archive to create
* @throws Exception if an exception occurs during the archive process
*/
public static void archive(File explodedArchive, File destArchive) throws Exception {
if (destArchive.exists()) {
throw new Exception("The destArchive " + destArchive + "already exists");
}
zip(explodedArchive, destArchive, true);
}
/**
* Zips a single file.
* @param prefix used to specify the parent directory of the file
* @param archiveDirectory the directory to be zipped
* @param zos the ZipOutputStream used for the zip process
* @throws IOException If an exception occurs during the zip process
*/
private static void zipOneFile(String prefix, File file, ZipOutputStream zos) throws IOException {
ZipEntry zipFileEntry = new ZipEntry(prefix + file.getName());
InputStream in = new BufferedInputStream(new FileInputStream(file.getAbsolutePath()));
int c;
zos.putNextEntry(zipFileEntry);
while ((c = in.read()) != -1) {
zos.write(c);
}
in.close();
zos.flush();
}
}
Related
- zip(File source, File target)
- zip(File source, File target)
- zip(File sourceDir, OutputStream targetStream)
- zip(File src, File target)
- zip(File srcDir, File zipFile)
- zip(File srcFile, File destFile, String archiveRoot)
- zip(File theFileToZip)
- zip(File toZip, File outFile)
- zip(File[] sourceFiles, File destFile, boolean deleteOriginalFiles)