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

U. Windl
  • 3,480
  • 26
  • 54
bastianneu
  • 2,059
  • 2
  • 18
  • 31
  • 5
    why not just copy the file itself, ignoring its contents? – kdgregory Dec 22 '09 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? – MHarris Dec 22 '09 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. – Fortega Dec 22 '09 at 13:24
  • so your idea is to seperate the copy and validation mechanism...right? – bastianneu Dec 22 '09 at 13:24

4 Answers4

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();
  }
}
Fortega
  • 19,463
  • 14
  • 75
  • 113
  • 2
    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 Dec 22 '09 at 13:18
  • I incorporated your comment in my answer :) – Fortega Dec 22 '09 at 13:21
  • 3
    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 Dec 22 '09 at 14:14
  • 1
    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 Dec 22 '09 at 14:15
  • Why is BUF_SIZE public? Why the null check in finally if fis and fos are always non-null? – Steve Kuo Dec 22 '09 at 17:22
6

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

Community
  • 1
  • 1
miku
  • 181,842
  • 47
  • 306
  • 310
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();
        }
    }
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();
    }
}
eeerahul
  • 1,629
  • 4
  • 27
  • 38
Nishanth Thomas
  • 696
  • 6
  • 4