I'm zipping up the contents of a directory, but running into an error when trying to open the zipped up files.
Can anyone tell what's going on with my code? Perhaps I'm not allocating enough bytes?
Look inside zipDirectory() and you will see that I'm zipping up folders which contain special extension files.
Not sure where the error's occurring, so maybe someone can help me out there!
Much appreciated
private void zipDirectory() {
File lazyDirectory = new File(defaultSaveLocation);
File[] files = lazyDirectory.listFiles();
for (File file : files) {
if (file.isDirectory()) {
System.out.println("Zipping up " + file);
zipContents(file);
}
}
}
public static void addToZip(String fileName, ZipOutputStream zos) throws FileNotFoundException, IOException {
System.out.println("Writing '" + fileName + "' to zip file");
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(fileName);
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zos.write(bytes, 0, length);
}
zos.closeEntry();
fis.close();
}
public static void zipContents(File dirToZip) {
List<File> fileList = new ArrayList<File>();
File[] filesToZip = dirToZip.listFiles();
for (File zipThis : filesToZip) {
String ext = "";
int i = zipThis.toString().lastIndexOf('.');
if (i > 0) {
ext = zipThis.toString().substring(i+1);
}
if(ext.matches("cpp|bem|gz|h|hpp|pl|pln|ppcout|vec|xml|csv")){
fileList.add(zipThis);
}
}
try {
FileOutputStream fos = new FileOutputStream(dirToZip.getName() + ".zip");
ZipOutputStream zos = new ZipOutputStream(fos);
for (File file : fileList) {
addToZip(file.toString(), zos);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ZIP files can get corrupted during the download process. If the download was interrupted, due to a power outage or an unexpected program closure even for a moment, unreadable data can end up becoming part of the downloaded ZIP file and make it difficult for the data to be extracted.
Your code is basically OK, try to find out which file is responsible for the corrupted zip file. Check whether digitalFile. getFile() always returns a valid and accessible argument to FileInputStream. Just add a bit logging to your code and you will find out what's wrong.
The java. util. zip. ZipFile class is used to read entries from a zip file.
Like most issues with IO streams in Java, your fault is almost certainly that you are not closing the streams properly. You need to add:
zos.finish(); // good practice
zos.close();
after the for loop.
For me the fix is that you need to do this for EVERY file entry
zos.finish()
zos.flush()
zos.closeEntry()
Then do the above things again to close the zos
. Otherwise, the default windows can't open the zipped folder properly, but third-party application works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With