tags:

views:

601

answers:

2

After some research:

How to create a Zip File

and some google research i came up with this java function:

 static void copyFile(File zipFile, File newFile) throws IOException {
    ZipFile zipSrc = new ZipFile(zipFile);
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(newFile));

    Enumeration srcEntries = zipSrc.entries();
    while (srcEntries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) srcEntries.nextElement();
            ZipEntry newEntry = new ZipEntry(entry.getName());
            zos.putNextEntry(newEntry);

            BufferedInputStream bis = new BufferedInputStream(zipSrc
                            .getInputStream(entry));

            while (bis.available() > 0) {
                    zos.write(bis.read());
            }
            zos.closeEntry();

            bis.close();
    }
    zos.finish();
    zos.close();
    zipSrc.close();
 }

This code is working...but it is not nice and clean at all...anyone got a nice idea or an example?

Edit:

I want to able to add some type of validation if the zip archive got the right structure...so copying it like an normal file without regarding its content is not working for me...or would you prefer checking it afterwards...i am not sure about this one

+6  A: 

Try: http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html#copyFile

The MYYN
I think my solution would be better. Reading and writing the files one character at a thime could be a bit slow...
Fortega
I give you a +1 for commons-io and a -1 for copying the file byte-by-byte.
Aaron Digulla
thime = time :-)
Fortega
kept the good part ..
The MYYN
+6  A: 

You just want to copy the complete zip file? Than it is not needed to open and read the zip file... Just copy it like you would copy every other file.

public final static int BUF_SIZE = 1024; //can be much bigger, see comment below


public static void copyFile(File in, File out) throws Exception {
  FileInputStream fis  = new FileInputStream(in);
  FileOutputStream fos = new FileOutputStream(out);
  try {
    byte[] buf = new byte[BUF_SIZE];
    int i = 0;
    while ((i = fis.read(buf)) != -1) {
        fos.write(buf, 0, i);
    }
  } 
  catch (Exception e) {
    throw e;
  }
  finally {
    if (fis != null) fis.close();
    if (fos != null) fos.close();
  }
}
Fortega
Don't hesitate to use a 10K or even 100K buffer. The memory isn't kept for long and it does make a difference for big files.
Aaron Digulla
I incorporated your comment in my answer :)
Fortega
Some comments on your code (without downvote): (1) If you're just going to rethrow the exception, there's no need for a catch block; try/finally is valid. (2) You should be creating both streams inside the try, otherwise you could throw while creating `fos` and leave `fis` open. (3) If the `close()` operations throw, the exceptions will mask any exception thrown from the try (which is probably of higher value). (4) By using `throws Exception`, you are throwing away valuable API documentation; the only checked exception that you'll get is `IOException`, so that should be what you declare.
kdgregory
All of which is a long way to say: "use Jakarata Commons IO" and don't rewrite code that you'll find there": http://commons.apache.org/io/api-release/org/apache/commons/io/IOUtils.html
kdgregory
Why is BUF_SIZE public? Why the null check in finally if fis and fos are always non-null?
Steve Kuo