Android 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
2010-11-05 00:54:57 来源:WEB开发网File srcFile, String prefixDir) throws IOException, FileNotFoundException {
ZipEntry zipEntry;
String filePath = srcFile.getAbsolutePath().replaceAll("\\", "/").replaceAll(
"//", "/");
if (srcFile.isDirectory()) {
filePath = filePath.replaceAll("/$", "") + "/";
}
String entryName = filePath.replace(prefixDir, "").replaceAll("/$", "");
if (srcFile.isDirectory()) {
if (!"".equals(entryName)) {
System.out.println("正在创建目录 - " + srcFile.getAbsolutePath()
+ " entryName=" + entryName);
//如果是目录,则需要在写目录后面加上 /
zipEntry = new ZipEntry(entryName + "/");
zos.putNextEntry(zipEntry);
}
File srcFiles[] = srcFile.listFiles();
for (int i = 0; i < srcFiles.length; i++) {
writeRecursive(zos, bo, srcFiles[i], prefixDir);
}
} else {
System.out.println("正在写文件 - " + srcFile.getAbsolutePath() + " entryName="
+ entryName);
BufferedInputStream bi = new BufferedInputStream(new FileInputStream(srcFile));
//开始写入新的ZIP文件条目并将流定位到条目数据的开始处
zipEntry = new ZipEntry(entryName);
zos.putNextEntry(zipEntry);
byte[] buffer = new byte[1024];
int readCount = bi.read(buffer);
while (readCount != -1) {
bo.write(buffer, 0, readCount);
readCount = bi.read(buffer);
}
//注,在使用缓冲流写压缩文件时,一个条件完后一定要刷新一把,不
//然可能有的内容就会存入到后面条目中去了
bo.flush();
//文件读完后关闭
bi.close();
}
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
更多精彩
赞助商链接