I was reading this to learn how to zip/unzip files using Java. I used this to guide me and it worked great when zipping all the files inside a folder, but when I tested it with a folder containing more folders inside of it, it didn't work, it threw the following error:
java.io.FileNotFoundException: assets (Access is denied) //assets is the name of the folder I tried to zip
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at Zip.main(Zip.java:24)
This is the class I'm using, as you will see it's the same Code Sample 4: Zip.java class code from the previous link:
import java.io.*;
import java.util.zip.*;
public class Zip {
static final int BUFFER = 2048;
public void zip() {
try {
BufferedInputStream origin = null;
FileOutputStream dest = new
FileOutputStream("H:\\myfigs.zip");
CheckedOutputStream checksum = new
CheckedOutputStream(dest, new Adler32());
ZipOutputStream out = new
ZipOutputStream(new
BufferedOutputStream(checksum));
//out.setMethod(ZipOutputStream.DEFLATED);
byte data[] = new byte[BUFFER];
// get a list of files from current directory
File f = new File(".");
String files[] = f.list();
for (int i=0; i<files.length; i++) {
System.out.println("Adding: "+files[i]);
FileInputStream fi = new
FileInputStream(files[i]);
origin = new
BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(files[i]);
out.putNextEntry(entry);
int count;
while((count = origin.read(data, 0,
BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
out.close();
System.out.println("checksum: "+checksum.getChecksum().getValue());
} catch(Exception e) {
e.printStackTrace();
}
}
}
What changes should be made so this code can zip folders inside folder and all of its files into a zip file?