reading zip file in java

Solutions on MaxInterview for reading zip file in java by the best coders in the world

showing results for - "reading zip file in java"
Marlene
15 Jan 2020
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\