-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompress.java
More file actions
30 lines (30 loc) · 978 Bytes
/
Copy pathCompress.java
File metadata and controls
30 lines (30 loc) · 978 Bytes
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
import java.util.*;
import java.io.File;
import java.io.*;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
class Compress {
public static void main(String[] args) throws IOException{
File toCompress = new File("testdir");
FileOutputStream fout = new FileOutputStream("compressed.zip");
ZipOutputStream zout = new ZipOutputStream(fout);
for(File file : toCompress.listFiles()) {
System.out.println("testdir"+File.separator+file.getName());
zipFile(zout, file);
}
zout.finish();
zout.close();
fout.close();
}
public static void zipFile(ZipOutputStream zout, File file) throws IOException{
FileInputStream fin = new FileInputStream("testdir"+ File.separator + file.getName());
ZipEntry nextEntry = new ZipEntry("testdir"+ File.separator + file.getName());
byte[] bytes = new byte[1024];
int len = 0;
zout.putNextEntry(nextEntry);
while ((len = fin.read(bytes)) >= 0) {
zout.write(bytes, 0, len);
}
fin.close();
}
}