By GokiSoft.com| 19:39 25/07/2022|
Java Advanced

[Source Code] Tìm hiểu nén và giải nén dữ liệu trong Java - C2108L



#Main.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson04;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.DeflaterInputStream;
import java.util.zip.InflaterInputStream;

/**
 *
 * @author QTA
 */
public class Main {
    public static void main(String[] args) {
//        zip();
        unzip();
    }
    
    static void unzip() {
        FileInputStream fis = null;
        InflaterInputStream iis = null;
        
        FileOutputStream fos = null;
        
        try {
            fis = new FileInputStream("data.dfl");
            iis = new InflaterInputStream(fis);
            
            fos = new FileOutputStream("data-output.txt");
            
            int code;
            while((code = iis.read()) != -1) {
                fos.write(code);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(iis != null) {
                try {
                    iis.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(fos != null) {
                try {
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    static void zip() {
        //Khai bao phan doc
        FileInputStream fis = null;
        DeflaterInputStream dis = null;
        //Khai bao ghi du lieu sau nen
        FileOutputStream fos = null;
        
        try {
            fis = new FileInputStream("data.txt");
            dis = new DeflaterInputStream(fis);
            
            fos = new FileOutputStream("data.dfl");
            
            int code;
            while((code = dis.read()) != -1) {
               fos.write(code);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(dis != null) {
                try {
                    dis.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if(fos != null) {
                try {
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}


Tags:



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó