Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java util zip creates "corrupt" zip files

Tags:

java

zip

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();
    }

enter image description here

like image 432
Mark Kennedy Avatar asked Jun 11 '13 23:06

Mark Kennedy


People also ask

Why do my zip files keep getting corrupted?

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.

How do you check if a zip file is corrupted in Java?

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.

What is Java Util zip ZipFile?

The java. util. zip. ZipFile class is used to read entries from a zip file.


2 Answers

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.

like image 149
rolfl Avatar answered Sep 21 '22 19:09

rolfl


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.

like image 20
cozyss Avatar answered Sep 20 '22 19:09

cozyss