3

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

4
  • 2
    Just out of curiosity, why not java.util.zip? Commented Apr 12, 2012 at 11:40
  • Please post a reproducible test case (possibly putting code on pastebin.com or similar). Without a test case, it's unlikely we can help. Commented Apr 12, 2012 at 11:40
  • I've been struggling with that for a long time now. I decided to use this. Commented Apr 12, 2012 at 11:40
  • 1
    Posting your example code elsewhere sort of lessens the value of the question as a stand-alone entry on this site. I've frequently found questions that were quite old to be very helpful. If the other site goes away or purges content then this question becomes incomplete. I would also try to post a smaller example, which would have a more focused question about why it doesn't work. Commented Apr 12, 2012 at 11:46

1 Answer 1

3

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) {
}
Sign up to request clarification or add additional context in comments.

3 Comments

I've used something like that before, but it always gives me a corrupted file.
I tried it, and it created a ZIP file, but didn't add anything into it.
out.putNextEntry(new ZipEntry(filenames[i])); this entry add files in zip have to create Array with filename, <it should be complete path>.. so the FileInputStream can be created....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.