0
我能夠壓縮文件夾,但不是遞歸的方式。請給我一個解決方案?如何在android中遞歸的Zip文件夾?
我能夠壓縮文件夾,但不是遞歸的方式。請給我一個解決方案?如何在android中遞歸的Zip文件夾?
作爲在Java(它是相同的)
static public void zipFolder(String srcFolder, String destZipFile)
throws Exception {
ZipOutputStream zip = null;
FileOutputStream fileWriter = null;
fileWriter = new FileOutputStream(destZipFile);
zip = new ZipOutputStream(fileWriter);
addFolderToZip("", srcFolder, zip);
zip.flush();
zip.close();
}
static private void addFileToZip(String path, String srcFile,
ZipOutputStream zip) throws Exception {
File folder = new File(srcFile);
if (folder.isDirectory()) {
addFolderToZip(path, srcFile, zip);
} else {
byte[] buf = new byte[1024];
int len;
FileInputStream in = new FileInputStream(srcFile);
zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
while ((len = in.read(buf)) > 0) {
zip.write(buf, 0, len);
}
}
}
static private void addFolderToZip(String path, String srcFolder,
ZipOutputStream zip) throws Exception {
File folder = new File(srcFolder);
for (String fileName : folder.list()) {
if (path.equals("")) {
addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);
} else {
addFileToZip(path + "/" + folder.getName(), srcFolder + "/"
+ fileName, zip);
}
}
}
這裏第一種方法(zipFolder)是主要的方法,其它的兩個簡單地壓縮的單個文件(addFileToZip)或文件夾(addFolderToZip)。
你的代碼看起來像壓縮單個目錄? – sarnold 2011-03-24 05:02:31