1.压缩文件或整个目录
// ZipCompression.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipCompression {
private String mFileDest;
private ZipOutputStream mZipOutputStream;
private static final String SEPARATOR = File.separator;
// public static void main(String[] args) {
//
// // ZipCompress zipCompress = new ZipCompress("E:\1.zip");
// ZipCompression zip = new ZipCompression("1.zip");
// zip.add("1.txt");
// zip.add(".");
// zip.add("2.txt");
// zip.close();
// }
/**
* @param pathname
* zip目标文件的名字
*/
public ZipCompression(String pathname) {
mFileDest = new File(pathname).getAbsolutePath();
FileOutputStream fos;
try {
fos = new FileOutputStream(mFileDest, true);
mZipOutputStream = new ZipOutputStream(fos);
} catch (FileNotFoundException e) {
printStackTrace(e);
}
}
/**
* 关闭zip文件,结束打包.
*/
public void close() {
if (mZipOutputStream != null) {
try {
mZipOutputStream.close();
} catch (IOException e) {
printStackTrace(e);
}
mZipOutputStream = null;
}
}
/**
* 添加一个文件或目录到zip文件
*
* @param filePath
* 待压缩的文件或目录,可以是相对目录
*/
public void add(String filePath) {
try {
File file = new File(filePath);
String path = "";
if (file.isDirectory()) {
filePath = file.getAbsolutePath();
if (filePath.endsWith("."))
filePath = filePath.substring(0, filePath.length() - 1);
if (filePath.endsWith(SEPARATOR))
filePath = filePath.substring(0, filePath.length() - 1);
// System.out.println("filePath:" + filePath);
int pos = filePath.lastIndexOf(SEPARATOR);
// System.out.println(filePath.substring(0, pos));
if (filePath.substring(0, pos).contains(SEPARATOR))
path = filePath.substring(pos + 1, filePath.length())
+ SEPARATOR;
// System.out.println("path:" + path);
}
byte[] buffer = new byte[1024];
add(mZipOutputStream, path, filePath, buffer);
} catch (Exception e) {
printStackTrace(e);
}
}
/**
* 添加一个文件或目录到zip文件,如果是目录则递归打包子目录
*
* @param zos
* zip压缩的目标文件
* @param path
* 待创建的zip文件夹内的相内路径
* @param file
* 待压缩的文件或目录的路径
* @param buffer
* 数据临时缓冲区
*/
private void add(ZipOutputStream zos, String path, String file,
byte[] buffer) {
try {
File inputFile = new File(file);
if (inputFile.isFile()) {
add(zos, path, inputFile, buffer);
} else if (inputFile.isDirectory()) {
// System.out.println("add dir:" + inputFile.getName());
for (File subFile : inputFile.listFiles()) {
if (subFile.isDirectory()) {
String newPath = path + subFile.getName() + SEPARATOR;
add(zos, newPath, subFile.getPath(), buffer);
} else {
add(zos, path, subFile, buffer);
}
}
}
} catch (Exception e) {
printStackTrace(e);
}
}
/**
* 添加一个已打开的文件到zip中
*
* @param zos
* zip压缩的目标文件
* @param path
* 待创建的zip文件夹内的相内路径
* @param file
* 待压缩的文件
* @param buffer
* 数据临时缓冲区
*/
private void add(ZipOutputStream zos, String path, File file, byte[] buffer) {
FileInputStream fis = null;
try {
path.equalsIgnoreCase("");
// 防止将目标zip文件打包进自己的压缩包内
String src = file.getAbsolutePath();
// System.out.println("src:" + src);
if (mFileDest.equalsIgnoreCase(src)) {
// System.out.println("Error! It's dest file! " + src);
return;
}
int ret;
try {
ZipEntry zipEntry = new ZipEntry(path + file.getName());
zos.putNextEntry(zipEntry);
FileInputStream fin = new FileInputStream(file);
while ((ret = fin.read(buffer)) != -1) {
zos.write(buffer, 0, ret);
}
fin.close();
zos.closeEntry();
} catch (Exception e) {
printStackTrace(e);
}
} catch (Exception e) {
printStackTrace(e);
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
printStackTrace(e);
}
}
}
private void printStackTrace(Exception exception) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
exception.printStackTrace(pw);
System.out.print(sw.toString());
// e.printStackTrace();
}
}
2.解压一个zip文件到指定的目录
// ZipDecompression.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class ZipDecompression {
private final String mZipFile;
private final String mDestPath;
public static void main(String[] args) {
ZipDecompression unzip = new ZipDecompression("e:\2.zip", "E:\2\");
unzip.start();
}
public ZipDecompression(String zip, String destPath) {
mZipFile = zip;
mDestPath = destPath;
}
/**
* 开始解压缩zip
*
* @return 成功返回true,出现任何错误返回false
*/
public boolean start() {
byte[] buffer = new byte[1024];
int ret = 0;
try {
ZipFile zipFile = new ZipFile(mZipFile);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
ZipEntry entry;
String name;
InputStream fis;
FileOutputStream fos;
while (entries.hasMoreElements()) {
entry = entries.nextElement();
name = mDestPath + entry.getName();
name = name.replaceAll("\*", "/"); // 替换会出现不成功!
// System.out.println("
" + name);
mkdirs(name);
fos = new FileOutputStream(name);
fis = zipFile.getInputStream(entry);
do {
ret = fis.read(buffer);
if (ret == -1)
break;
fos.write(buffer, 0, ret);
} while (true);
fis.close();
fos.close();
}
} catch (FileNotFoundException e) {
printStackTrace(e);
return false;
} catch (IOException e) {
printStackTrace(e);
return false;
} catch (Exception e) {
printStackTrace(e);
return false;
}
return true;
}
/**
* 递归创建目录
*
* @param pathname
* 文件或目录名称
*/
private void mkdirs(String pathname) {
try {
File file = new File(pathname);
if (!file.isDirectory()) {
for (int i = pathname.length() - 1; i >= 0; i--) {
Character c = pathname.charAt(i);
if (c.equals('\') || c.equals('/')) {
String newPath = pathname.substring(0, i);
// System.out.println(newPath);
file = new File(newPath);
break;
}
}
}
if (!file.exists())
file.mkdirs();
} catch (Exception e) {
printStackTrace(e);
}
}
private static void printStackTrace(Exception exception) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
exception.printStackTrace(pw);
System.out.print(sw.toString());
// e.printStackTrace();
}
}