การบีบอัดไฟล์แบบ ZIP ใน Java

การบีบอัดไฟล์แบบ ZIP ใน Java นั้นเราใช้ "java.util.zip" เพื่อใช้ในการบีบอัดไฟล์ครับ หลักการทั่วที่ใช้อยู่คือ
1. อ่านข้อมูลไฟล์ด้วย "FileInputStream" 
2. กำหนดชื่อให้ "ZipEntry" และเขียนไฟล์ออกด้วย "ZipOutputStream" ครับ

ตัวอย่าง 
1. ตัวอย่างการ ZIP ไฟล์ ทำการอ่านไฟล์ "C:\\users\\nopphanan7\\Directory1\\photo.jpg" และทำการบีบอัดไฟล์ชื่อ "C:\\users\\nopphanan7\\MyFile.zip" ออกมา.
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
package demo.compress;
 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
 
public class CompressFileExample {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        byte[] buffer = new byte[1024];
 
        try {
 
            FileOutputStream fos = new FileOutputStream("C:\\users\\nopphanan7\\MyFile.zip");
            ZipOutputStream zos = new ZipOutputStream(fos);
            ZipEntry ze = new ZipEntry("photo.jpg");
            zos.putNextEntry(ze);
            FileInputStream in = new FileInputStream("C:\\users\\nopphanan7\\Directory1\\photo.jpg");
 
            int len;
            while ((len = in.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }
            in.close();
            zos.closeEntry();
            // remember close it
            zos.close();
            System.out.println("Compress file is done");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
ผลลัพธ์ที่ได้คือ
1
Compress file is done

2. ตัวอย่างเพิ่มเติม การบีบอัดไฟล์แบบ ZIP โดยการ Recursively อ่านข้อมูลไฟล์ทั้งหมดในโฟรเดอร์ "C:\\users\\nopphanan7\\Directory1" และทำการบีบอัดไฟล์ออกมาในชื่อ "C:\\users\\nopphanan7\\Directory1.zip".
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package demo.compress;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
 
public class CompressFileExample2 {
 
    List<string> fileList;
    private static final String OUTPUT_ZIP_FILE = "C:\\users\\nopphanan7\\Directory1.zip";
    private static final String SOURCE_FOLDER = "C:\\users\\nopphanan7\\Directory1";
 
    CompressFileExample2() {
        fileList = new ArrayList<string>();
    }
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        CompressFileExample2 appZip = new CompressFileExample2();
        appZip.generateFileList(new File(SOURCE_FOLDER));
        appZip.zipIt(OUTPUT_ZIP_FILE);
    }
 
    /**
     * Zip it
     * @param zipFile
     *            output ZIP file location
     */
    public void zipIt(String zipFile) {
 
        byte[] buffer = new byte[1024];
 
        try {
 
            FileOutputStream fos = new FileOutputStream(zipFile);
            ZipOutputStream zos = new ZipOutputStream(fos);
 
            System.out.println("Output to Zip : " + zipFile);
 
            for (String file : this.fileList) {
 
                System.out.println("File Added : " + file);
                ZipEntry ze = new ZipEntry(file);
                zos.putNextEntry(ze);
 
                FileInputStream in = new FileInputStream(SOURCE_FOLDER
                        + File.separator + file);
 
                int len;
                while ((len = in.read(buffer)) > 0) {
                    zos.write(buffer, 0, len);
                }
 
                in.close();
            }
 
            zos.closeEntry();
            // remember close it
            zos.close();
 
            System.out.println("Compress file is done");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
 
    /**
     * Traverse a directory and get all files, and add the file into fileList
     *
     * @param node
     *            file or directory
     */
    public void generateFileList(File node) {
 
        // add file only
        if (node.isFile()) {
            fileList.add(generateZipEntry(node.getAbsoluteFile().toString()));
        }
 
        if (node.isDirectory()) {
            String[] subNote = node.list();
            for (String filename : subNote) {
                generateFileList(new File(node, filename));
            }
        }
 
    }
 
    /**
     * Format the file path for zip
     *
     * @param file
     *            file path
     * @return Formatted file path
     */
    private String generateZipEntry(String file) {
        return file.substring(SOURCE_FOLDER.length() + 1, file.length());
    }
}
</string></string>
ผลลัพธ์ที่ได้คือ
1
2
3
4
5
6
7
Output to Zip : C:\users\nopphanan7\Directory1.zip
File Added : FIO.jpg
File Added : photo.jpg
File Added : photo2.jpg
File Added : the_dark_knight.jpg
File Added : ubuntu-black-1440x900.jpg
Compress 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