การคลายไฟล์จากไฟล์ ZIP ใน Java

ต่อไปนี้เป็นหัวข้อเกี่ยวกับการคลายไฟล์จากไฟล์ ZIP ใน Java ชึ่งมีหลักการทั่วไปดังนี้. 

1. อ่าน ZIP ไฟล์ด้วย "ZipInputStream"
2. ดึงไฟล์ต่างๆให้ "ZipEntry" จัดการจากนั้นเขียนไฟล์ออกด้วย "FileOutputStream" 1

ตัวอย่าง
ในตัวอย่างเราจะทำการอ่านไฟล์ zip จาก "C:\\users\\nopphanan7\\Directory1.zip" และทำการคลาย zip ไปไว้ในโฟรเดอร์ "C:\\users\\nopphanan7\\outputzip".
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package demo.compress;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
 
public class DecompressZIPExample {
 
    List<string> fileList;
    private static final String INPUT_ZIP_FILE = "C:\\users\\nopphanan7\\Directory1.zip";
    private static final String OUTPUT_FOLDER = "C:\\users\\nopphanan7\\outputzip";
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        DecompressZIPExample unZip = new DecompressZIPExample();
        unZip.unZipIt(INPUT_ZIP_FILE, OUTPUT_FOLDER);
 
    }
 
    /**
     * Unzip it
     *
     * @param zipFile
     *            input zip file
     * @param output
     *            zip file output folder
     */
    public void unZipIt(String zipFile, String outputFolder) {
 
        byte[] buffer = new byte[1024];
 
        try {
 
            // create output directory is not exists
            File folder = new File(OUTPUT_FOLDER);
            if (!folder.exists()) {
                folder.mkdir();
            }
 
            // get the zip file content
            ZipInputStream zis = new ZipInputStream(
                    new FileInputStream(zipFile));
            // get the zipped file list entry
            ZipEntry ze = zis.getNextEntry();
 
            while (ze != null) {
 
                String fileName = ze.getName();
                File newFile = new File(outputFolder + File.separator
                        + fileName);
 
                System.out.println("file unzip : " + newFile.getAbsoluteFile());
 
                // create all non exists folders
                // else you will hit FileNotFoundException for compressed folder
                new File(newFile.getParent()).mkdirs();
 
                FileOutputStream fos = new FileOutputStream(newFile);
 
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
 
                fos.close();
                ze = zis.getNextEntry();
            }
 
            zis.closeEntry();
            zis.close();
 
            System.out.println("Decompress zip file is done.");
 
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
</string>
ผลลัพธ์ที่ได้คือ
1
2
3
4
5
6
file unzip : C:\users\nopphanan7\outputzip\FIO.jpg
file unzip : C:\users\nopphanan7\outputzip\photo.jpg
file unzip : C:\users\nopphanan7\outputzip\photo2.jpg
file unzip : C:\users\nopphanan7\outputzip\the_dark_knight.jpg
file unzip : C:\users\nopphanan7\outputzip\ubuntu-black-1440x900.jpg
Decompress zip file is done.

About Nop

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment