1import java.io.FileOutputStream;
2import java.io.IOException;
3import java.nio.file.*;
4import java.nio.file.attribute.BasicFileAttributes;
5import java.util.zip.ZipEntry;
6import java.util.zip.ZipOutputStream;
7
8public class ZipCompress {
9 public static void compress(String dirPath) {
10 final Path sourceDir = Paths.get(dirPath);
11 String zipFileName = dirPath.concat(".zip");
12 try {
13 final ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(zipFileName));
14 Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() {
15 @Override
16 public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {
17 try {
18 Path targetFile = sourceDir.relativize(file);
19 outputStream.putNextEntry(new ZipEntry(targetFile.toString()));
20 byte[] bytes = Files.readAllBytes(file);
21 outputStream.write(bytes, 0, bytes.length);
22 outputStream.closeEntry();
23 } catch (IOException e) {
24 e.printStackTrace();
25 }
26 return FileVisitResult.CONTINUE;
27 }
28 });
29 outputStream.close();
30 } catch (IOException e) {
31 e.printStackTrace();
32 }
33 }
34}
1import java.io.BufferedOutputStream;
2import java.io.File;
3import java.io.FileOutputStream;
4import java.nio.file.Files;
5import java.nio.file.Paths;
6import java.util.ArrayList;
7import java.util.List;
8import java.util.zip.ZipEntry;
9import java.util.zip.ZipOutputStream;
10
11public class zip {
12
13 public static void main(String[] args) {
14 zipFolder(mapFolder("Test"));
15
16 System.out.println("Done");
17 }
18
19 public static List<String> mapFolder(String path, boolean includeEmptyFolders) {
20 List<String> map = new ArrayList<String>();
21 List<String> unmappedDirs = new ArrayList<String>();
22 File[] items = new File(path).listFiles();
23
24 if (!path.substring(path.length() - 1).equals("/")) {
25 path += "/";
26 }
27
28 if (items != null) {
29 for (File item : items) {
30 if (item.isFile()) {
31 map.add(path+item.getName());
32 } else {
33 unmappedDirs.add(path+item.getName());
34 }
35 }
36
37 if (!unmappedDirs.isEmpty()) {
38 for (String folder : unmappedDirs) {
39 List<String> temp = mapFolder(folder, includeEmptyFolders);
40 if (!temp.isEmpty()) {
41 for (String item : temp)
42 map.add(item);
43 } else if (includeEmptyFolders == true) {
44 map.add(folder+"/");
45 }
46 }
47 }
48 }
49 return map;
50 }
51
52 public static void zipFolder(String zipPath, List<String> items) {
53 try {
54 FileOutputStream f = new FileOutputStream(zipPath);
55 ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(f));
56
57 for (String item : items) {
58 String contents = String.join("\n", Files.readAllLines(Paths.get(item)));
59 zip.putNextEntry(new ZipEntry(item));
60
61 byte[] data = contents.getBytes();
62 zip.write(data, 0, data.length);
63 zip.closeEntry();
64 }
65
66 zip.close();
67 f.close();
68 } catch(Exception e) {
69 System.out.println(e.getMessage());
70 }
71 }
72}
1import java.io.BufferedInputStream;
2import java.io.FileInputStream;
3import java.io.FileOutputStream;
4import java.io.IOException;
5import java.io.InputStream;
6import java.util.Date;
7import java.util.Enumeration;
8import java.util.zip.ZipEntry;
9import java.util.zip.ZipFile;
10import java.util.zip.ZipInputStream;
11
12/**
13 * Java program to iterate and read file entries from Zip archive.
14 * This program demonstrate two ways to retrieve files from Zip using ZipFile and by using ZipInputStream class.
15 * @author Javin
16 */
17
18public class ZipFileReader {
19
20 // This Zip file contains 11 PNG images
21 private static final String FILE_NAME = "C:\\temp\\pics.zip";
22 private static final String OUTPUT_DIR = "C:\\temp\\Images\\";
23 private static final int BUFFER_SIZE = 8192;
24
25 public static void main(String args[]) throws IOException {
26
27 // Prefer ZipFile over ZipInputStream
28 readUsingZipFile();
29 // readUsingZipInputStream();
30
31 }
32
33 /*
34 * Example of reading Zip archive using ZipFile class
35 */
36
37 private static void readUsingZipFile() throws IOException {
38 final ZipFile file = new ZipFile(FILE_NAME);
39 System.out.println("Iterating over zip file : " + FILE_NAME);
40
41 try {
42 final Enumeration<? extends ZipEntry> entries = file.entries();
43 while (entries.hasMoreElements()) {
44 final ZipEntry entry = entries.nextElement();
45 System.out.printf("File: %s Size %d Modified on %TD %n", entry.getName(), entry.getSize(), new Date(entry.getTime()));
46 extractEntry(entry, file.getInputStream(entry));
47 }
48 System.out.printf("Zip file %s extracted successfully in %s", FILE_NAME, OUTPUT_DIR);
49 } finally {
50 file.close();
51 }
52
53 }
54
55 /*
56 * Example of reading Zip file using ZipInputStream in Java.
57 */
58
59 private static void readUsingZipInputStream() throws IOException {
60 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(FILE_NAME));
61 final ZipInputStream is = new ZipInputStream(bis);
62
63 try {
64 ZipEntry entry;
65 while ((entry = is.getNextEntry()) != null) {
66 System.out.printf("File: %s Size %d Modified on %TD %n", entry.getName(), entry.getSize(), new Date(entry.getTime()));
67 extractEntry(entry, is);
68 }
69 } finally {
70 is.close();
71 }
72
73 }
74
75 /*
76 * Utility method to read data from InputStream
77 */
78
79 private static void extractEntry(final ZipEntry entry, InputStream is) throws IOException {
80 String exractedFile = OUTPUT_DIR + entry.getName();
81 FileOutputStream fos = null;
82
83 try {
84 fos = new FileOutputStream(exractedFile);
85 final byte[] buf = new byte[BUFFER_SIZE];
86 int read = 0;
87 int length;
88
89 while ((length = is.read(buf, 0, buf.length)) >= 0) {
90 fos.write(buf, 0, length);
91 }
92
93 } catch (IOException ioex) {
94 fos.close();
95 }
96
97 }
98
99}
100
101Output:
102Iterating over zip file : C:\temp\pics.zip
103File: Image (11).png Size 21294 Modified on 10/24/13
104File: Image (1).png Size 22296 Modified on 11/19/13
105File: Image (2).png Size 10458 Modified on 10/24/13
106File: Image (3).png Size 18425 Modified on 11/19/13
107File: Image (4).png Size 31888 Modified on 11/19/13
108File: Image (5).png Size 27454 Modified on 11/19/13
109File: Image (6).png Size 67608 Modified on 11/19/13
110File: Image (7).png Size 8659 Modified on 11/19/13
111File: Image (8).png Size 40015 Modified on 11/19/13
112File: Image (9).png Size 17062 Modified on 10/24/13
113File: Image (10).png Size 42467 Modified on 10/24/13
114Zip file C:\temp\pics.zip extracted successfully in C:\temp\Images\