-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZipUtil.java
More file actions
51 lines (43 loc) · 1.31 KB
/
ZipUtil.java
File metadata and controls
51 lines (43 loc) · 1.31 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
private static String FILEPATH = "/home/martin/JavaFileZipper/";
private static String FILE = "compress.txt";
private static String FILETOZIP = "helloWorld.zip";
static ZipGUI g;
public static void main (String [] args) {
g = new ZipGUI();
}
public static void ZipFile() {
byte[] buffer = new byte[1024];
FILEPATH = g.getFileLocation();
FILE = g.getFileName();
FILETOZIP = g.getZipFileName();
System.out.println("FILEPATH : " + FILEPATH);
System.out.println("FILE : " + FILE);
System.out.println("FILETOZIP : " + FILETOZIP);
if (FILEPATH == null || FILE == null || FILETOZIP == null) {
g.checkFileNames();
}
try {
FileOutputStream fos = new FileOutputStream(FILEPATH + "/"+ FILETOZIP);
ZipOutputStream zos = new ZipOutputStream(fos);
ZipEntry ze = new ZipEntry(FILE);
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(FILEPATH +"/"+ FILE);
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
zos.closeEntry();
zos.close();
System.out.println("Done!");
} catch (IOException e) {
e.printStackTrace();
}
}
}