compress folder
hello
i wrote this code to compress directory with subdirectories but it through this exception if the directory contains more than one directory but it works fine with any number of subdirectories for example (C:\Test\New Folder\Copy of New Folder\New Folder\New Folder\New Folder\New Folder\test.java)
publicvoid compressDirectory(boolean firstTime, String directoryPath){
try{
if (firstTime){
String zipfile = directoryPath +".zip";
out =new ZipOutputStream(new FileOutputStream(zipfile));
}
File dir =new File(directoryPath);
String[] entries = dir.list();
byte[] buffer =newbyte[4096];
int bytes_read;
for (int i = 0; i < entries.length; i++){
File f =new File(dir, entries[i]);
if (f.isDirectory())
compressDirectory(false, f.getAbsolutePath());
else{
in =new FileInputStream(f);
ZipEntry entry =new ZipEntry(f.getPath());
out.putNextEntry(entry);
while ((bytes_read = in.read(buffer)) != -1)
out.write(buffer, 0, bytes_read);
}
}
in.close();
out.close();
}catch (Exception c){
c.printStackTrace();
}
}
Exception :
--
java.io.IOException: Stream closed
at java.util.zip.ZipOutputStream.ensureOpen(Unknown Source)
at java.util.zip.ZipOutputStream.putNextEntry(Unknown Source)
at Compress.compressDirectory(Compress.java:46)
at Compress.compressDirectory(Compress.java:42)
at Test.main(Test.java:6)
also i have another problem : Empty folders are ignored
thanks

