java压缩解压需

package com.apache.jhp.test;

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.Enumeration;

import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

public class ApacheCommon {

/**
* 解压文件
*
* @param zipFileName
*            要解压的文件
* @param extPlace
*            解压后的输出目录
* @throws Exception
*/
public static void upzip(String zipFileName, String extPlace)
throws Exception {
if (zipFileName == null || extPlace == null) {
throw new Exception("");
}
ZipFile zipFile = new ZipFile(zipFileName);
Enumeration e = zipFile.getEntries();
ZipEntry zipEntry = null;
InputStream in = null;
OutputStream os = null;
File tempDir = null;
File extPlaceFile = new File(extPlace);
String tempName = "";
// 如果解压目录是 d://jhp 则
if (!extPlace.endsWith("/")) {
extPlace = extPlace + "/";
}
// 如果解压文件夹不存在
if (!extPlaceFile.isDirectory()) {
extPlaceFile.mkdir();
}
int x = 0;
try {
while (e.hasMoreElements()) {
x++;
zipEntry = (ZipEntry) e.nextElement();
String entryName = zipEntry.getName();
if (zipEntry.isDirectory()) {
tempDir = new File(extPlace + entryName);
if (!tempDir.exists()) {

tempDir.mkdir();
}
} else {
if (entryName.indexOf("/") != -1) {
tempName = entryName.substring(0, entryName
.lastIndexOf("/"));
tempDir = new File(extPlace + tempName);
if (!tempDir.exists()) {

tempDir.mkdir();
}

}
in = zipFile.getInputStream(zipEntry);
os = new FileOutputStream(new File(extPlace + entryName));
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
os.write(buf, 0, len);
}


}

}
} catch (Exception e1) {
throw new Exception("io exception!"
+ e1.getStackTrace()[0].getLineNumber());
} finally {
in.close();
os.close();
zipFile.close();
}
}

/**
* 压缩。
*
* @param src源文件或者目录
* @param dest压缩文件路径
* @throws IOException
*/

public static void zip(String src, String dest) throws IOException {
long startTime = System.currentTimeMillis();

ZipOutputStream out = null;
try {
File outFile = new File(dest);
out = new ZipOutputStream(outFile);
File fileOrDirctory = new File(src);

zipFileOrDirectory(out, fileOrDirctory, "");
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException ex) {
}
}
}

}

/**
* 递归压缩文件或目录
*
* @param out
*            压缩输出流对象
* @param fileOrDirectory
*            要压缩的文件或目录对象
* @param curPath
*            当前压缩条目的路径,用于指定条目名称的前缀
* @throws IOException
*/
private static void zipFileOrDirectory(ZipOutputStream out,
File fileOrDirectory, String curPath) throws IOException {
FileInputStream in = null;
try {

if (!fileOrDirectory.isDirectory()) {

// 压缩文件
byte[] buffer = new byte[4096];
int bytes_read;
in = new FileInputStream(fileOrDirectory);

ZipEntry entry = new ZipEntry(curPath
+ fileOrDirectory.getName());
out.putNextEntry(entry);

while ((bytes_read = in.read(buffer)) != -1) {
out.write(buffer, 0, bytes_read);
}
out.closeEntry();
} else {

// 压缩目录
File[] entries = fileOrDirectory.listFiles();
for (int i = 0; i < entries.length; i++) {
// 递归压缩,更新curPaths
zipFileOrDirectory(out, entries[i], curPath
+ fileOrDirectory.getName() + "/");
}
}
} catch (IOException ex) {
ex.printStackTrace();
throw ex;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
}
}
}
}

public static void main(String[] args) {
try {
// upzip2("d://aports.zip", "d://pppppp119/");
zip("d://pppppp119", "d://aaaaa.zip");
System.out.println("sucess!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

你可能感兴趣的:(java,apache,ant,OS)