דחיסת קובץ באמצעות DeflaterOutputStream
מחלקה זו מיישמת מסנן זרם פלט לדחיסת נתונים בפורמט הדחיסה 'deflate'. הוא משמש גם כבסיס לסוגים אחרים של מסנני דחיסה כגון GZIPOutputStream. שיטות חשובות:- קח קובץ קלט 'קובץ 1' ל-FileInputStream לקריאת נתונים.
- קח את קובץ הפלט 'קובץ 2' והקצה אותו ל-FileOutputStream. זה יעזור לכתוב נתונים לתוך 'קובץ2'.
- הקצה FileOutputStream ל-DeflaterOutputStream עבור דחיסת הנתונים.
- כעת קרא נתונים מ- FileInputStream וכתוב אותם לתוך DeflaterOutputStream. זה ידחוס את הנתונים וישלח אותם ל-FileOutputStream שמאחסן את הנתונים הדחוסים בקובץ הפלט.
- קובץ עם השם 'file2' מכיל כעת נתונים דחוסים ועלינו להשיג נתונים מקוריים מפורקים מהקובץ הזה.
- הקצה את הקובץ הדחוס 'file2' ל-FileInputStream. זה עוזר לקרוא נתונים מ'file2'.
- הקצה את קובץ הפלט 'file3' ל-FileOutputStream. זה יעזור לכתוב נתונים לא דחוסים לתוך 'קובץ3'.
- כעת קרא נתונים לא דחוסים מ-InflaterInputStream וכתוב אותם לתוך FileOutputStream. זה יכתוב את הנתונים הלא דחוסים ל'file3'.
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.Deflater; import java.util.zip.DeflaterOutputStream; class zip { public static void main(String[] args) throws IOException { //Assign the original file : file to //FileInputStream for reading data FileInputStream fis=new FileInputStream('file1'); //Assign compressed file:file2 to FileOutputStream FileOutputStream fos=new FileOutputStream('file2'); //Assign FileOutputStream to DeflaterOutputStream DeflaterOutputStream dos=new DeflaterOutputStream(fos); //read data from FileInputStream and write it into DeflaterOutputStream int data; while ((data=fis.read())!=-1) { dos.write(data); } //close the file fis.close(); dos.close(); } }
ביטול דחיסה של קובץ באמצעות InflaterInputStream
מחלקה זו מיישמת מסנן זרמים לביטול דחיסה של נתונים בפורמט הדחיסה 'לרוקן'. הוא משמש גם כבסיס למסנני דקומפרסיה אחרים כגון GZIPInputStream. שיטות חשובות:import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.InflaterInputStream; //Uncompressing a file using an InflaterInputStream class unzip { public static void main(String[] args) throws IOException { //assign Input File : file2 to FileInputStream for reading data FileInputStream fis=new FileInputStream('file2'); //assign output file: file3 to FileOutputStream for reading the data FileOutputStream fos=new FileOutputStream('file3'); //assign inflaterInputStream to FileInputStream for uncompressing the data InflaterInputStream iis=new InflaterInputStream(fis); //read data from inflaterInputStream and write it into FileOutputStream int data; while((data=iis.read())!=-1) { fos.write(data); } //close the files fos.close(); iis.close(); } }
צור חידון