-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleFileZip.java
More file actions
34 lines (27 loc) · 1.05 KB
/
SingleFileZip.java
File metadata and controls
34 lines (27 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package java_Files;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import java.io.FileOutputStream;
public class SingleFileZip {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
File file = new File("c:/temp/abc.txt"); // 定义要压缩的文件
File zipFile = new File("c:/temp/single2.zip"); // 定义压缩文件的名称
InputStream input = new FileInputStream(file); // 定义文件的输入流
ZipOutputStream zipOut = null; // 声明压缩流对象
zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
zipOut.putNextEntry(new ZipEntry(file.getName())); // 设置ZipEntry对象
zipOut.setComment("single file zip"); // 设置注释
// 压缩过程
int temp = 0;
while ((temp = input.read()) != -1) { // 读取内容
zipOut.write(temp); // 压缩输出
}
input.close(); // 关闭输入流
zipOut.close(); // 关闭输出流
System.out.println("single file zip done.");
}
}