java zip压缩文件

 1 package com.ssh.remote.common.utils;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileOutputStream;
 6 import java.io.InputStream;
 7 import java.util.zip.ZipEntry;
 8 import java.util.zip.ZipOutputStream;
 9 
10 /**
11  * @author liuxn
12  * @date 2019/12/9
13  **/
14 public class ZipUtils {
15     private static byte[] _byte = new byte[1024];
16 
17     public static File zipFile(File srcFile) throws Exception {
18         String zip = srcFile.getParent() + "/" + srcFile.getName() + ".zip";
19         File file = new File(zip);
20         ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(file));
21         InputStream _in = new FileInputStream(srcFile);
22         zipOut.putNextEntry(new ZipEntry(srcFile.getName()));
23         int len = 0;
24         while ((len = _in.read(_byte)) > 0) {
25             zipOut.write(_byte, 0, len);
26         }
27         _in.close();
28         zipOut.closeEntry();
29         zipOut.close();
30         return file;
31 
32     }
33 
34     public static void main(String[] args) throws  Exception{
35         File file = new File("C:/Users/Desktop/备份/remote.json");
36         zipFile(file);
37     }
38 
39 
40 }

猜你喜欢

转载自www.cnblogs.com/lxn0216/p/12009952.html