Creating zip archive in Java

前端 未结 7 2080
一整个雨季
一整个雨季 2020-11-28 06:20

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

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 06:49

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

提交回复
热议问题