我正在做一個簡單的android應用程序,它由壓縮文件夾組成。實際上壓縮過程已完成,文件保存在已定義的位置。但問題是,當我從模擬器中壓縮壓縮文件,並手動解壓縮文件損壞。有什麼問題。手動解壓縮時文件是否被破壞?android壓縮文件夾
我的文件夾結構如下
TestPlay1-給出包含兩個子目錄 - 播放列表和內容
目錄播放列表包含一個XML文件
目錄內容包含文件,如圖片和視頻
我下面
String zipFile = "/mnt/sdcard/Testplay1.zip";
byte[] buffer = new byte[1024];
FileOutputStream fos = null;
try {
fos = new FileOutputStream(zipFile);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
ZipOutputStream zos = new ZipOutputStream(fos);
updata = new ArrayList<File>();
contentpath = new File(FOLDER_PATH);
try {
if (contentpath.isDirectory()) {
File[] listFile = contentpath.listFiles();
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
File[] contfile = listFile[i].listFiles();
for (int j = 0; j < contfile.length; j++) {
updata.add(contfile[j]);
FileInputStream fis = new FileInputStream(contfile[j]);
zos.putNextEntry(new ZipEntry(contfile[j].getName()));
int length;
while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
zos.closeEntry();
// close the InputStream
fis.close();
}
}
}
zos.close();
}
System.out.println("Testplay1 Folder contains=>" + updata);
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
您應該考慮使用Apache的壓縮librairies完成。無需重新發明任何這些常見操作.. http://developer-tips.hubpages.com/hub/Zipping-and-Unzipping-Nested-Directories-in-Java-using-Apache-Commons-Compress – Snicolas