Hello everyone.
I have asked this question so that you could give me a hand in the obstacle that has been presented to me on my way.
My problem
My problem is that I want to be able to compress the data or the contents of a folder in a zip, but my current compression code has not given me a positive or expected result. For the reason that it does not compress the desired thing
My code
public class Comp {
public static void zipDir(OutputStream zipFilename, String dir) throws Exception {
File dirObj = new File(dir);
ZipOutputStream out = new ZipOutputStream(zipFilename);
System.out.println("Creating : ");
addDir(dirObj, out);
out.close();
}
static void addDir(File dirObj, ZipOutputStream out) throws IOException {
File[] files = dirObj.listFiles();
byte[] tmpBuf = new byte[1024];
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
addDir(files[i], out);
continue;
}
FileInputStream in = new FileInputStream(files[i].getAbsolutePath());
System.out.println(" Adding: " + files[i].getAbsolutePath());
out.putNextEntry(new ZipEntry(files[i].getAbsolutePath()));
int len;
while ((len = in.read(tmpBuf)) > 0) {
out.write(tmpBuf, 0, len);
}
out.closeEntry();
in.close();
}
}
}
Explanation: My code is very simple, I just pass two parameters, one that is the ouputStream where it can be written and the other is where I passed the path of the folder where I copied the content
And I call these methods from the activity where the compression process will be triggered in the following way:
File file = new File(bb);
OutputStream os = driveContents1.getOutputStream();
Comp comp=new Comp();
try {
comp.zipDir(os, file.getAbsolutePath());
Toast.makeText(this, "Se COmprimimio", Toast.LENGTH_LONG).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
If you notice where I fail in my code or know other ways to achieve my goal. Please communicate it to me. Thanks.