Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy and validate a Zip File via Java

Tags:

java

zip

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

like image 693
bastianneu Avatar asked Dec 22 '09 13:12

bastianneu


People also ask

How can I check if a zip file is valid?

To access the test function, open the Unzip tab (the Zip pane must be the active pane). Click the top part of the Diagnostics button to test the Zip file and view a summary report.

How do you check if a zip file is corrupted in Java?

Your code is basically OK, try to find out which file is responsible for the corrupted zip file. Check whether digitalFile. getFile() always returns a valid and accessible argument to FileInputStream. Just add a bit logging to your code and you will find out what's wrong.

How can I read the content of a zip file without unzipping it in Java?

Methods. getComment(): String – returns the zip file comment, or null if none. getEntry(String name): ZipEntry – returns the zip file entry for the specified name, or null if not found. getInputStream(ZipEntry entry) : InputStream – Returns an input stream for reading the contents of the specified zip file entry.


3 Answers

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();
  }
}
like image 176
Fortega Avatar answered Sep 19 '22 00:09

Fortega


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

like image 38
miku Avatar answered Sep 20 '22 00:09

miku


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();
    }
}
like image 35
Nishanth Thomas Avatar answered Sep 20 '22 00:09

Nishanth Thomas