ตัวอย่าง GZip
ในตัวอย่างจะทำการบีบอัดไฟล์ "C:\\users\\nopphanan7\\Directory1\\photo.jpg" ไปเป็น gzip ไฟล์ชื่อ "C:\\users\\nopphanan7\\MyGZFile.gz".
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 | package demo.compress; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.GZIPOutputStream; public class CompressGZipFileExample { private static final String OUTPUT_GZIP_FILE = "C:\\users\\nopphanan7\\MyGZFile.gz" ; private static final String SOURCE_FILE = "C:\\users\\nopphanan7\\Directory1\\photo.jpg" ; /** * @param args */ public static void main(String[] args) { CompressGZipFileExample gZip = new CompressGZipFileExample(); gZip.gzipIt(); } /** * GZip it * * @param zipFile output GZip file location */ public void gzipIt() { byte [] buffer = new byte [ 1024 ]; try { GZIPOutputStream gzos = new GZIPOutputStream( new FileOutputStream(OUTPUT_GZIP_FILE)); FileInputStream in = new FileInputStream(SOURCE_FILE); int len; while ((len = in.read(buffer)) > 0 ) { gzos.write(buffer, 0 , len); } in.close(); gzos.finish(); gzos.close(); System.out.println( "Compress file with GZip is Done" ); } catch (IOException ex) { ex.printStackTrace(); } } } |
1 | Compress file with GZip is Done |
0 comments:
Post a Comment