6

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

4
  • 5
    why not just copy the file itself, ignoring its contents? Commented Dec 22, 2009 at 13:12
  • Why not check the structure first, and then do a regular file copy (which won't incur the overhead of compressing and uncompressing every file in the zip) if it passes? Commented Dec 22, 2009 at 13:22
  • 1
    Regarding your edit: As long as you don't want to add, change or remove files to the zip archive, the best (and fastest) solution would be to check whether it really is a valid zip archive, and afterwards copying it like you would copy a normal file. Commented Dec 22, 2009 at 13:24
  • so your idea is to seperate the copy and validation mechanism...right? Commented Dec 22, 2009 at 13:24

4 Answers 4

10

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

5 Comments

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.
I incorporated your comment in my answer :)
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.
All of which is a long way to say: "use Jakarata Commons IO" and don't rewrite code that you'll find there": commons.apache.org/io/api-release/org/apache/commons/io/…
Why is BUF_SIZE public? Why the null check in finally if fis and fos are always non-null?
6

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

2 Comments

I think my solution would be better. Reading and writing the files one character at a thime could be a bit slow...
I give you a +1 for commons-io and a -1 for copying the file byte-by-byte.
1

My solution:

import java.io.*;
import javax.swing.*;
public class MovingFile
{
    public static void copyStreamToFile() throws IOException
    {
        FileOutputStream foutOutput = null;
        String oldDir =  "F:/UPLOADT.zip";
        System.out.println(oldDir);
        String newDir = "F:/NewFolder/UPLOADT.zip";  // name as the destination file name to be done
        File f = new File(oldDir);
        f.renameTo(new File(newDir));
    }
    public static void main(String[] args) throws IOException
    {
        copyStreamToFile();
    }
}

Comments

1

I have updated your code to Java 9+, FWIW

   try (ZipFile srcFile = new ZipFile(inputName)) {
        try (ZipOutputStream destFile = new ZipOutputStream(
                Files.newOutputStream(Paths.get(new File(outputName).toURI())))) {
            Enumeration<? extends ZipEntry> entries = srcFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry src = entries.nextElement();
                ZipEntry dest = new ZipEntry(src.getName());
                destFile.putNextEntry(dest);
                try (InputStream content = srcFile.getInputStream(src)) {
                    content.transferTo(destFile);
                }
                destFile.closeEntry();
            }
            destFile.finish();
        }
    }

Comments

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.