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

ก่อนหน้านั้นเราเรียนรู้เรื่องการบีบอัดไฟล์ในแบบ GZip แล้วต่อไปนี้เราลองมาดูวิธีการคลายไฟล์ดังกล่าวกันครับ.
ตัวอย่าง Gzip 
ในตัวอย่างจะทำการคลายไฟล์ Gzip "C:\\users\\nopphanan7\\MyGZFile.gz" ให้ไปเป็น "C:\\users\\nopphanan7\\photo.jpg".
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
package demo.compress;
 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
 
public class DecompressGZipFileExample {
 
    private static final String INPUT_GZIP_FILE = "C:\\users\\nopphanan7\\MyGZFile.gz";
    private static final String OUTPUT_FILE = "C:\\users\\nopphanan7\\photo.jpg";
 
    public static void main(String[] args) {
        DecompressGZipFileExample gZip = new DecompressGZipFileExample();
        gZip.gunzipIt();
    }
 
    /**
     * GunZip it
     */
    public void gunzipIt() {
        byte[] buffer = new byte[1024];
        try {
 
            GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(INPUT_GZIP_FILE));
            FileOutputStream out = new FileOutputStream(OUTPUT_FILE);
 
            int len;
            while ((len = gzis.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
 
            gzis.close();
            out.close();
 
            System.out.println("Decompress file with GZip is Done");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
ผลลัพธ์ที่ได้คือ
1
Decompress file with GZip 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