Creating an empty java zip file

I am currently writing a function that will create a zip file that will be used in other functions. Below is my function 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 tried to debug, but I could not find the source of this problem. The function runs without any additional problems, but the created zip file is empty.

+4
source share
6 answers

You must close the record using ZipOutputStream # closeEntry () before closing the output stream, or the record never confirms that it has been completely written.

Also, the name ZipEntry cannot be the whole path; that is, it should be dog.png instead of C:\Users\Admin\Documents\dog.png . This problem will go away without exception and will lead to data compression from the file directly to zip, and not to zip as a compressed file.

+6
source

Well, just wondering why you pass the file name as a parameter if you don't use it in the code. Since you always use this.filename. This makes me think that you are trying to name the zip file with the name that you set to the state of the objects, and since you are also using the same name in ZipEntry, trying to add the same zipper file to it .. since ZipEntry must specify an existing file, therefore it becomes empty.

Hope this helps.

+2
source
 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(); } } 
+2
source

Try flushing the buffer with zip.flush () before closing, although close should flush the buffer.

Also check this.filename. You have a local variable with the same name. The local variable filename is never used. Perhaps the zip file is being written to a different place than you expect.

0
source

@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(); } } 

this code creates a zip file and it works for me too. :)

0
source

A simple solution. Make one manual directory in ZipEntry without a file separator.

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

instead

 zip.putNextEntry(new ZipEntry(fileName)); 

Here fileName = file.getAbsoluteFile();

First, the LOG directory will be created in the zip file, and then fileNames with the directory. This avoids creating the initial empty directory in the zip file.

0
source

All Articles