ZipUtils-压缩工具类
支持中文的解压工具类。
更新于:2015-08-10
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
public class ZipUtils {
/**
* 解压zip包,支持中文。
* 需要ant.jar包,下载地址:http://download.csdn.net/detail/qiantujava/8984345
*
* @param zipFile 需要解压的文件,如:/mnt/sdcard/abc/abc.zip
* @param zipFileDir 需要解压的文件所在的文件夹,如:/mnt/sdcard/abc/
* @throws IOException
*/
public static void unZipFile(String zipFile, String zipFileDir)
throws IOException {
BufferedInputStream bi;
ZipFile zf = new ZipFile(zipFile, "GBK");
Enumeration e = zf.getEntries();
while (e.hasMoreElements()) {
ZipEntry entry = (ZipEntry) e.nextElement();
String entryName = entry.getName();
String path = zipFileDir + "/" + entryName;
if (entry.isDirectory()) {
File dir = new File(path);
if (!dir.exists()) dir.mkdirs();
} else {
String fileDir = path.substring(0, path.lastIndexOf("/"));
File fileDirFile = new File(fileDir);
if (!fileDirFile.exists()) fileDirFile.mkdirs();
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(zipFileDir + "/" + entryName));
bi = new BufferedInputStream(zf.getInputStream(entry));
byte[] readContent = new byte[1024];
int readCount = bi.read(readContent);
while (readCount != -1) {
bos.write(readContent, 0, readCount);
readCount = bi.read(readContent);
}
bos.close();
}
}
zf.close();
}
}
版权声明:本文为博主原创文章,转载请注明原地址。
时间: 2024-12-20 18:39:36