cheese5505
cheese5505

Reputation: 982

Java creating corrupted ZIP file

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

Upvotes: 3

Views: 2504

Answers (1)

Dhruv
Dhruv

Reputation: 10693

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) {
}

Upvotes: 3

Related Questions