Reputation: 1076
I am doing a simple android application which consists of compressing folder. Actually the compression process is completed and the file is saved in the defined location. But the problem is when i push the zipped file from the emulator and unzipping it manually the files are corrupted. What's the problem. Is the file become corrupted when we unzip manually?
My folder structure is given below
TestPlay1- contains two sub directories - playlist and content
the directory playlist contains a xml file
the directory content contains files such as images and videos
My code is given below
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
}
Upvotes: 2
Views: 1558
Reputation: 5417
I suggest you to use Zip4j: http://www.lingala.net/zip4j/
There you can just put the Folder to compress and the rest is done by the library
ZipFile zipfile = new ZipFile("/mnt/sdcard/bla.zip");
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
zipfile.addFolder("/mnt/sdcard/folderToZip", parameters);
Upvotes: 3