Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java creating corrupted ZIP file

Tags:

java

file

zip

I am using zt-zip by zeroturnaround, and when I compress it, and I try to open it, it says it is corrupted. Any ideas? ZipUtil.pack(new File("C:\\Users\\David"), new File(zipName)); http://pastie.org/3773634

like image 508
cheese5505 Avatar asked Feb 26 '26 14:02

cheese5505


1 Answers

To make a Zip file you can use directly following java class

import java.util.zip.ZipFile;

// These are the files to include in the ZIP file
String[] filenames = new String[]{"filename1", "filename2"};

// Create a buffer for reading the files
byte[] buf = new byte[1024];

try {
    // Create the ZIP file
    String outFilename = "outfile.zip";
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));

    // Compress the files
    for (int i=0; i<filenames.length; i++) {
        FileInputStream in = new FileInputStream(filenames[i]);

        // Add ZIP entry to output stream.
        out.putNextEntry(new ZipEntry(filenames[i]));

        // Transfer bytes from the file to the ZIP file
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }

        // Complete the entry
        out.closeEntry();
        in.close();
    }

    // Complete the ZIP file
    out.close();
} catch (IOException e) {
}
like image 176
Dhruv Bansal Avatar answered Feb 28 '26 04:02

Dhruv Bansal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!