Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating zip archive in Java

Tags:

java

zip

I have one file created by 7zip program. I used deflate method to compress it. Now I want to create the same archive (with the same MD5sum) in java. When I create zip file, I used the algorithm that I found on the Internet for example http://www.kodejava.org/examples/119.html but when I created zip file with this method the compressed size is higher than size of the uncompressed file so what is going on? This isn't a very useful compression. So how I can create zip file that is exactly same as zip file that I created with 7zip program ? If it helps I have all information about zip file that I created in 7zip program.

like image 220
hudi Avatar asked Jan 23 '11 12:01

hudi


People also ask

How do you make a zip file with Java?

Steps to Compress a File in JavaOpen a ZipOutputStream that wraps an OutputStream like FileOutputStream. The ZipOutputStream class implements an output stream filter for writing in the ZIP file format. Put a ZipEntry object by calling the putNextEntry(ZipEntry) method on the ZipOutputStream.

How do I create an archive zip file?

Press and hold (or right-click) the file or folder, select (or point to) Send to, and then select Compressed (zipped) folder. A new zipped folder with the same name is created in the same location.

How do you use ZipOutputStream in Java?

write(byte[] buf, int off, int len) method writes an array of bytes to the current ZIP entry data. This method will block until all the bytes are written.


1 Answers

// simplified code for zip creation in java  import java.io.*; import java.util.zip.*;  public class ZipCreateExample {      public static void main(String[] args) throws Exception {          // input file          FileInputStream in = new FileInputStream("F:/sometxt.txt");          // out put file          ZipOutputStream out = new ZipOutputStream(new FileOutputStream("F:/tmp.zip"));          // name the file inside the zip  file          out.putNextEntry(new ZipEntry("zippedjava.txt"));           // buffer size         byte[] b = new byte[1024];         int count;          while ((count = in.read(b)) > 0) {             out.write(b, 0, count);         }         out.close();         in.close();     } } 
like image 176
Nishanth Thomas Avatar answered Oct 11 '22 12:10

Nishanth Thomas