By GokiSoft.com| 20:09 17/08/2021|
Java Advanced

[Share Code] Share Code - Hướng dẫn tạo dự án WinRar, WinZip, 7Zip toàn tập bằng Java - C2010L


- String -> nen & giai nen - File -> nen -> luu 1 file moi (nen) - File(nen) -> giai nen -> luu 1 file moi (giai nen) ================================================================ I) Nen file 1) Data Source (txt) -> Doc du lieu (FileInputStream) -> Nen du lieu (DeflaterInputStream) -> data (nen) -> Luu du lieu (FileOutputStream) -> Destination (zip) 2) Data Source (txt) -> Doc du lieu (FileInputStream) -> data (chua nen) -> DeflaterOutputStream (Nen) -> Luu du lieu (FileOutputStream) -> Destination Data (zip) II) Giai nen File 1) Data Source (zip) -> Doc du lieu (FileInputStream) -> Giai nen (InflaterInputStream) -> data (giai nen) -> Luu du lieu (FileOutputStream) -> Destination (unzip) 2) Data Source (zip) -> Doc du lieu (FileInputStream) -> data (giai nen) -> Giai nen (InflaterOutputStream) -> Luu du lieu (FileOutputStream) -> Destination (unzip)




#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 java2.lesson03;

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.DeflaterOutputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        //testZipText();
        //testZipFile1();
        //testZipFile2();
        testUnzipFile1();
    }
    
    static void testUnzipFile1() {
        FileInputStream fis = null;
        InflaterInputStream iis = null;
        FileOutputStream fos = null;
        
        try {
            fis = new FileInputStream("data.zip");
            iis = new InflaterInputStream(fis);
            
            fos = new FileOutputStream("data-unzip.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 testZipFile2() {
        FileInputStream fis = null;
        DeflaterOutputStream dos = null;
        FileOutputStream fos = null;
        
        try {
            fis = new FileInputStream("data.txt");
            
            fos = new FileOutputStream("data2.zip");
            dos = new DeflaterOutputStream(fos);
            
            int code;
            while((code = fis.read()) != -1) {
                dos.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(dos != null) {
                try {
                    dos.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 testZipFile1() {
        FileInputStream fis = null;
        DeflaterInputStream dis = null;
        FileOutputStream fos = null;
        
        try {
            fis = new FileInputStream("data.txt");
            dis = new DeflaterInputStream(fis);
            
            fos = new FileOutputStream("data.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(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);
                }
            }
        }
    }
    
    static void testZipText() {
        try {
            String str = "Viết chương trình quản lý sinh viên. Mỗi đối tượng sinh viên có các thuộc tính sau: id, name, age, address và gpa (điểm trung bình). Yêu cầu: tạo ra một menu với các chức năng sau:Viết chương trình quản lý sinh viên. Mỗi đối tượng sinh viên có các thuộc tính sau: id, name, age, address và gpa (điểm trung bình). Yêu cầu: tạo ra một menu với các chức năng sau:Viết chương trình quản lý sinh viên. Mỗi đối tượng sinh viên có các thuộc tính sau: id, name, age, address và gpa (điểm trung bình). Yêu cầu: tạo ra một menu với các chức năng sau:Viết chương trình quản lý sinh viên. Mỗi đối tượng sinh viên có các thuộc tính sau: id, name, age, address và gpa (điểm trung bình). Yêu cầu: tạo ra một menu với các chức năng sau:Viết chương trình quản lý sinh viên. Mỗi đối tượng sinh viên có các thuộc tính sau: id, name, age, address và gpa (điểm trung bình). Yêu cầu: tạo ra một menu với các chức năng sau:Viết chương trình quản lý sinh viên. Mỗi đối tượng sinh viên có các thuộc tính sau: id, name, age, address và gpa (điểm trung bình). Yêu cầu: tạo ra một menu với các chức năng sau:Viết chương trình quản lý sinh viên. Mỗi đối tượng sinh viên có các thuộc tính sau: id, name, age, address và gpa (điểm trung bình). Yêu cầu: tạo ra một menu với các chức năng sau:Viết chương trình quản lý sinh viên. Mỗi đối tượng sinh viên có các thuộc tính sau: id, name, age, address và gpa (điểm trung bình). Yêu cầu: tạo ra một menu với các chức năng sau:Viết chương trình quản lý sinh viên. Mỗi đối tượng sinh viên có các thuộc tính sau: id, name, age, address và gpa (điểm trung bình). Yêu cầu: tạo ra một menu với các chức năng sau:Viết chương trình quản lý sinh viên. Mỗi đối tượng sinh viên có các thuộc tính sau: id, name, age, address và gpa (điểm trung bình). Yêu cầu: tạo ra một menu với các chức năng sau:Viết chương trình quản lý sinh viên. Mỗi đối tượng sinh viên có các thuộc tính sau: id, name, age, address và gpa (điểm trung bình). Yêu cầu: tạo ra một menu với các chức năng sau:";
            byte[] originalData = str.getBytes("utf8");
            System.out.println("Du lieu goc: " + str.length());
            
            //Chuong trinh nen
            Deflater deflater = new Deflater();
            deflater.setInput(originalData);
            deflater.finish();
            
            byte[] zipData = new byte[1000];
            int length = deflater.deflate(zipData);
            
            System.out.println("zip length: " + length);
            System.out.println("zip string: " + new String(zipData, 0, length));
            
            //Chuong trinh giai nen
            Inflater inflater = new Inflater();
            inflater.setInput(zipData, 0, length);
            
            byte[] unzipData = new byte[3000];
            length = inflater.inflate(unzipData);
            
            System.out.println("Sau giai nen: " + length);
            System.out.println(new String(unzipData, 0, length));
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (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)