ZipUtils


import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Utility to Zip and Unzip nested directories recursively.
*
* @author Robin Spark
*/
public class ZipUtils {

private static Logger logger = LoggerFactory.getLogger(ZipUtils.class);

/**
* Creates a zip file at the specified path with the contents of the
* specified directory. NB:
*
* @param sourceDirectoryPath
*            The path of the directory where the archive will be created.
*            eg. c:/temp
* @param targetZipFilePath
*            The full path of the archive to create. eg.
*            c:/temp/archive.zip
* @throws IOException
*             If anything goes wrong
*/
public static void createZip(
String sourceDirectoryPath,
String targetZipFilePath) throws IOException
{
FileOutputStream fOut = null;
BufferedOutputStream bOut = null;
ZipArchiveOutputStream tOut = null;

try {
fOut = new FileOutputStream(new File(targetZipFilePath));
bOut = new BufferedOutputStream(fOut);
tOut = new ZipArchiveOutputStream(bOut);
addFileToZip(tOut, sourceDirectoryPath, "");
} finally {
tOut.finish();
tOut.close();
bOut.close();
fOut.close();
}

}

public static void createZip(
List<File> fileList,
String targetZipFilePath) throws IOException
{
FileOutputStream fOut = null;
BufferedOutputStream bOut = null;
ZipArchiveOutputStream tOut = null;

try {
fOut = new FileOutputStream(new File(targetZipFilePath));
bOut = new BufferedOutputStream(fOut);
tOut = new ZipArchiveOutputStream(bOut);
addFileToZip(tOut, fileList);
} finally {
tOut.finish();
tOut.close();
bOut.close();
fOut.close();
}

}

public static void createZip(
Map<String, File> fileMap,
String targetZipFilePath) throws Exception
{
FileOutputStream fOut = null;
BufferedOutputStream bOut = null;
ZipArchiveOutputStream tOut = null;

try {
fOut = new FileOutputStream(new File(targetZipFilePath));
bOut = new BufferedOutputStream(fOut);
tOut = new ZipArchiveOutputStream(bOut);
addFileToZip(tOut, fileMap);
} finally {
String encoding = tOut.getEncoding();

if (logger.isDebugEnabled()) {
logger.debug("tOut encoding:" + encoding);
}

if (logger.isDebugEnabled()) {
logger.debug("tOut.finish()---begin");
}

tOut.finish();

if (logger.isDebugEnabled()) {
logger.debug("tOut.finish()---end");
}

tOut.close();
bOut.close();
fOut.close();
}

}

private static void addFileToZip(
ZipArchiveOutputStream zOut,
List<File> fileList) throws IOException
{

for (File f : fileList) {

String entryName = f.getName();
ZipArchiveEntry zipEntry = new ZipArchiveEntry(f, entryName);
zOut.putArchiveEntry(zipEntry);
FileInputStream fInputStream = null;
try {
fInputStream = new FileInputStream(f);
IOUtils.copy(fInputStream, zOut);
zOut.closeArchiveEntry();
} catch (Exception e) {
e.printStackTrace();
} finally {
// fInputStream.close();
org.apache.commons.io.IOUtils.closeQuietly(fInputStream);
}
}

}

private static void addFileToZip(
ZipArchiveOutputStream zOut,
Map<String, File> fileMap) throws Exception
{

if (MapUtils.isEmpty(fileMap)) {
return;
}

for (Entry<String, File> entry : fileMap.entrySet()) {
String entryName = entry.getKey();
File f = entry.getValue();
ZipArchiveEntry zipEntry = new ZipArchiveEntry(f, entryName);
zOut.putArchiveEntry(zipEntry);

if (logger.isDebugEnabled()) {

logger.debug("put zipEntry:" + zipEntry.getName());
}

FileInputStream fInputStream = null;
try {
fInputStream = new FileInputStream(f);
IOUtils.copy(fInputStream, zOut);
zOut.closeArchiveEntry();

if (logger.isDebugEnabled()) {

logger.debug("close zipEntry:" + zipEntry.getName());
}

} catch (Exception e) {
throw e;
} finally {
fInputStream.close();
// org.apache.commons.io.IOUtils.closeQuietly(fInputStream);
}
}

}

/**
* Creates a zip entry for the path specified with a name built from the
* base passed in and the file/directory name. If the path is a directory, a
* recursive call is made such that the full directory is added to the zip.
*
* @param zOut
*            The zip file's output stream
* @param path
*            The filesystem path of the file/directory being added
* @param base
*            The base prefix to for the name of the zip file entry
*
* @throws IOException
*             If anything goes wrong
*/
private static void addFileToZip(
ZipArchiveOutputStream zOut,
String path,
String base) throws IOException
{
File f = new File(path);
String entryName = base + f.getName();
ZipArchiveEntry zipEntry = new ZipArchiveEntry(f, entryName);

zOut.putArchiveEntry(zipEntry);

if (f.isFile()) {
FileInputStream fInputStream = null;
try {
fInputStream = new FileInputStream(f);
IOUtils.copy(fInputStream, zOut);
zOut.closeArchiveEntry();
} finally {
fInputStream.close();
}

} else {
zOut.closeArchiveEntry();
File[] children = f.listFiles();

if (children != null) {
for (File child : children) {
addFileToZip(zOut, child.getAbsolutePath(), entryName + "/");
}
}
}
}

/**
* Method for testing zipping and unzipping.
*
* @param args
*/
public static void main(
String[] args) throws Exception
{
// ZipUtils.createZip("c:/MyFile", "C:/解压缩MyFile-123.zip");

// List<File> fileList = new ArrayList<File>();
//
// fileList.add(new File("C:\\MyFile\\spy.log"));
//
// fileList.add(new File("C:\\MyFile\\中华人民共和国.log"));

Map<String, File> fileMap = new HashMap<String, File>();

fileMap.put("xxx.TXT", new File("C:\\MyFile\\spy.log"));

fileMap.put("yyy.LOG", new File("C:\\MyFile\\中华人民共和国.log"));

ZipUtils.createZip(fileMap, "C:/解压缩MyFile-12345.zip");

System.out.println("Done...");
}
}

你可能感兴趣的:(util)