java解压缩-3-实用_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java解压缩-3-实用

java解压缩-3-实用

 2013/8/23 18:57:27  vernonchen163  程序员俱乐部  我要评论(0)
  • 摘要:importjava.io.BufferedOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importjava.util
  • 标签:Java 实用 压缩 解压缩

?

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Map.Entry;

?

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;

import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;

import org.apache.commons.compress.utils.IOUtils;

?

/**

?* Utility to Zip and Unzip nested directories recursively.

?*?

?* @author Robin Spark

?*/

public class ZipUtils {

?

/**

* 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 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, fileMap);

} finally {

tOut.finish();

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();

}

}

?

}

?

private static void addFileToZip(

ZipArchiveOutputStream zOut,

Map<String, File> fileMap) throws IOException

{

?

for (Entry<String, File> entry : fileMap.entrySet()) {

?

String entryName = entry.getKey();

File f = entry.getValue();

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();

}

}

?

}

?

/**

* 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 IOException

{

// 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...");

}

}

发表评论
用户名: 匿名