By GokiSoft.com| 10:11 09/09/2021|
Java Advanced

[Share Code] Viết chương trình nén và giải nén file (WinRar, WinZip, 7Zip) - Lập trình Java nâng cao



Giải thuật nén FILE

1) Data Source (gokisoft.txt) -> đầu đọc data (FileInputStream) -> DeflaterInputStream (Bộ nén) -> dữ liệu đc nén -> Lưu dữ liệu (FileOutputStream) -> Zip File (gokisoft.zip)
2) Data Source (gokisoft.txt) -> đầu đọc data (FileInputStream) -> dữ liệu không nén -> DeflaterOutputStream (Bộ nén) -> Lưu dữ liệu (FileOutputStream) -> Zip File (gokisoft.zip)

Giải nén FILE
1) Zip File (gokisoft.zip) -> đầu đọc data (FileInputStream) -> InflaterInputStream (Bộ giải nén) -> dữ liệu đã đc giải nén -> Lưu dữ liệu (FileOutputStream) -> Unzip File (gokisoft2.txt)
2) Zip File (gokisoft.zip) -> đầu đọc data (FileInputStream) -> dữ liệu nén -> InflaterOutputStream (Bộ giải nén) -> Lưu dữ liệu (FileOutputStream) -> Unzip File (gokisoft2.txt)




#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.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.DeflaterInputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;

/**
 * Giải thuật nén FILE
    1) Data Source (gokisoft.txt) -> đầu đọc data (FileInputStream) -> DeflaterInputStream (Bộ nén) -> dữ liệu đc nén -> Lưu dữ liệu (FileOutputStream) -> Zip File (gokisoft.zip)
    2) Data Source (gokisoft.txt) -> đầu đọc data (FileInputStream) -> dữ liệu không nén -> DeflaterOutputStream (Bộ nén) -> Lưu dữ liệu (FileOutputStream) -> Zip File (gokisoft.zip)

    Giải nén FILE
    1) Zip File (gokisoft.zip) -> đầu đọc data (FileInputStream) -> InflaterInputStream (Bộ giải nén) -> dữ liệu đã đc giải nén -> Lưu dữ liệu (FileOutputStream) -> Unzip File (gokisoft2.txt)
    2) Zip File (gokisoft.zip) -> đầu đọc data (FileInputStream) -> dữ liệu nén -> InflaterOutputStream (Bộ giải nén) -> Lưu dữ liệu (FileOutputStream) -> Unzip File (gokisoft2.txt)
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        //testZipUnzipText();
        //Nen FILE
        //zipFile();
        unZipFile();
    }
    
    static void unZipFile() {
        FileInputStream fis = null;
        InflaterInputStream iis = null;
        FileOutputStream fos = null;
        
        try {
            fis = new FileInputStream("gokisoft.zip");
            iis = new InflaterInputStream(fis);
            
            fos = new FileOutputStream("gokisoft2.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(iis != null) {
                try {
                    iis.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(fis != null) {
                try {
                    fis.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 zipFile() {
        FileInputStream fis = null;
        DeflaterInputStream dis = null;
        FileOutputStream fos = null;
        
        try {
            fis = new FileInputStream("gokisoft.txt");
            dis = new DeflaterInputStream(fis);
            
            fos = new FileOutputStream("gokisoft.zip");
            
            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(dis != null) {
                try {
                    dis.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(fis != null) {
                try {
                    fis.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 testZipUnzipText() {
        String str = "Lớp C2009G - 54 Lê Thanh Nghị - Xin Chào.";
        try {
            //Nen du lieu
            byte[] originalData = str.getBytes("utf8");
            System.out.println("original length: " + originalData.length);
            
            Deflater deflater = new Deflater();
            deflater.setInput(originalData);
            deflater.finish();
            
            byte[] outputData = new byte[1000];
            int length = deflater.deflate(outputData);
            System.out.println("length: " + length);
            
            //Giai nen du lieu
            Inflater inflater = new Inflater();
            inflater.setInput(outputData);
            byte[] infData = new byte[1000];
            length = inflater.inflate(infData);
            
            System.out.println("inf length: " + length);
            String s = new String(infData, 0, length);
            System.out.println(s);
        } catch (UnsupportedEncodingException | DataFormatException 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)

Hieu Ngo [community,C2009G]
Hieu Ngo

2021-09-09 03:51:49


#Main.java


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.DeflaterInputStream;
//Bai 1
public class Main {
    public static void main(String [] args) {
        zipFile();
    }
    static void zipFile() {
        FileInputStream fis = null;
        DeflaterInputStream dis = null;
        FileOutputStream fos = null;
        try {
            try {
                fis = new FileInputStream("abc.txt");
                dis = new DeflaterInputStream(fis);
                fos = new FileOutputStream("vidu.data");

                int code;
                while((code = dis.read()) != -1){
                    fos.write(code);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } finally {
            if(fis!=null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos!=null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(dis !=null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


#Main.java


import java.io.File;
import java.util.Scanner;
//Bai 2 
public class Main {
    static void duyetFile(File file) {
        if(file.isFile()) {
            System.out.println("-File "+ file.getName());
        } else if(file.isDirectory()){
            System.out.println("-Folder "+ file.getName());
        }
        if(file.isDirectory()) {
            File[] files = file.listFiles();
            for(File file1 : files) {
                Main.duyetFile(file1);
            }
        }
    }
    public static void main(String [] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Nhap ten folder:");
        String f = input.nextLine();
        File folder = new File(f);
        if(!folder.exists() || !folder.isDirectory()) {
            System.out.println("Folder does not exist");
            return;
        }
        duyetFile(folder);
    }
}