Creating zip archive in Java

前端 未结 7 2122
一整个雨季
一整个雨季 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:50

    package comm;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;*emphasized text*
    import java.io.IOException;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;
    
    public class Zip1 {
          public static void main( String[] args )
            {
                byte[] buffer = new byte[1024];
    
                try{
    
                    File f= new  File("E:\\");
                    f.mkdirs();
                    File origFile= new File(f,"MyZipFile2.zip");
                    FileOutputStream fos = new FileOutputStream(origFile);
    
                    ZipOutputStream zos = new ZipOutputStream(fos);
                    ZipEntry ze= new ZipEntry("test.pdf");
                    zos.putNextEntry(ze);
                    FileInputStream in = new FileInputStream("D:\\Test.pdf");
    
                    int len;
                    while ((len = in.read(buffer)) > 0) {
                        zos.write(buffer, 0, len);
                    }
    
                    in.close();
                    zos.closeEntry();
    
                    //remember close it
                    zos.close();
    
                    System.out.println("Done");
    
                }catch(IOException ex){
                   ex.printStackTrace();
                }
            }
    }
    

提交回复
热议问题