cybertextron
cybertextron

Reputation: 10961

producing an empty zip file java

I'm currently writing a function what would create a zip file, which will be used in other functionality. Below it is my function's code:

public void createZip(){

        try{
            String outfile = this.filename + ".zip";
            //input file
            FileInputStream input = new FileInputStream(this.filename);
            //output file
            ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(outfile));
            //name the file inside the zip file
            System.out.println(this.filename);
            zip.putNextEntry(new ZipEntry(this.filename));

            byte[] buffer = new byte[this.BUFFER];
            int len;
            //copy the file to the zip
            while((len= input.read(buffer)) > 0){
                System.out.println(len);
                zip.write(buffer, 0, len);
            }
            zip.closeEntry();
            zip.flush();
            input.close();
            zip.close();
            this.filename += ".zip";
        }
        catch(IOException e){
            e.printStackTrace();
        }

    }

I have tried to debug, but I couldn't find the source of this problem. The function runs without any further problems, but the zip file produced it is an empty one.

Upvotes: 4

Views: 12955

Answers (6)

npocmaka
npocmaka

Reputation: 57252

final static byte[] EmptyZip={80,75,05,06,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00};
public static void createEmptyZip(String path){
    try{
        FileOutputStream fos=new FileOutputStream(new File(path));
        fos.write(EmptyZip, 0, 22);
        fos.flush();
        fos.close();
    }catch (FileNotFoundException e){
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

Upvotes: 3

Chirag Katudia
Chirag Katudia

Reputation: 511

Simple solution. Make one manual directory in ZipEntry without File Separator.

zip.putNextEntry(new ZipEntry("LOG" + fileName));

instead of

zip.putNextEntry(new ZipEntry(fileName));

Here fileName = file.getAbsoluteFile();

This will first create LOG dir in zip file followed by fileNames with directory path. This avoids creating initial empty directory in zip file.

Upvotes: 0

FThompson
FThompson

Reputation: 28687

You must close the entry using ZipOutputStream#closeEntry() prior to closing the output stream, or the entry is never confirmed to have been written entirely.

Also, the name of the ZipEntry cannot be the entire path; i.e, it should be dog.png instead of C:\Users\Admin\Documents\dog.png. This issue will pass by without an exception, and will cause the data from the file to be compressed directly into the zip, rather than into the zip as a compressed file.

Upvotes: 10

hekomobile
hekomobile

Reputation: 1388

@phillipe try this please

    public void createZip(String filename) {
    try {
        //input file
        FileInputStream input = new FileInputStream(filename);
        //output file
        ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(filename + ".zip"));
        //name the file inside the zip file
        zip.putNextEntry(new ZipEntry(filename));

        byte[] buffer = new byte[1024];
        int len;
        //copy the file to the zip
        while((len = input.read(buffer)) > 0) {
            System.out.println();
            zip.write(buffer, 0 , len);
        }
        zip.closeEntry();
        zip.flush();
        zip.close();
        input.close();
        filename += ".zip";
    } catch(IOException e) {
        e.printStackTrace();
    }
}

those code create a zip file and that's work and for me too. :)

Upvotes: 0

Ed Morales
Ed Morales

Reputation: 1037

well, just wondering why you pass a filename as a parameter if you dont use it within the code.. Since you are always using the this.filename. That makes me think that you are trying to name a zip file with a name you set into the objects state and since you are also using that same name in the ZipEntry its trying to add that same zipper file inside it.. since the ZipEntry must point to an existing file, thats why it comes up empty.

Hope it helps.

Upvotes: 2

tjg184
tjg184

Reputation: 4676

Try to flush the buffer by using zip.flush() before closing, although close should flush the buffer.

Also verify this.filename. You have a local variable with the same name. The local variable filename is never used. It's possible the zip file is being written to a different location than what you expect.

Upvotes: 0

Related Questions