By GokiSoft.com|
20:59 06/05/2020|
Java Advanced
Share Code - Hướng dẫn tạo dự án WinRar, WinZip, 7Zip toàn tập bằng Java
Share Code - Hướng dẫn tạo dự án WinRar, WinZip, 7Zip toàn tập bằng Java
Hướng dẫn
Học cách xây dựng 1 project => WinRar, WinZip, 7Zip
Ứng dụng:
=> Nén dữ liệu => Large => Smaller
=> Nén dữ liệu => Smaller => Sử dụng nó để gửi qua internet
Làm thế nào chúng nén dữ liệu
A => a => Inflater
a => A => Deflater
Loại 1: Nén biến dữ liệu
String str = "Học cách xây dựng 1 project => WinRar, WinZip, 7Zip"
=> dữ liệu nén
=> giải nén
Loại 2: Nén File (A => a) & giải nén File (a => A)
- Chú ý: Khi các bạn sử dụng giải thuật nén
- Dữ liệu sau khi nén sẽ bao gồm
1. Thông tin nen => Được sử dụng cho sau này để giải nén dữ liệu => Chiến 1 phần data nhất đinh
2. Chứa dữ liệu nén
=> Với data cần nén nhỏ => sau khi nén có khi kích thước của nó sẽ lơn hơn so với lúc đầu => dữ liệu nhỏ => ko nên sử dụng nén
Khi data cần nén có size lớn => Chúng ta sẽ thấy tác dụng của giải thuật nén.
Loại 2-1: Nén File
File (text) => File (Được nén, Zip) thì làm như nào
Thread 1>> File (Source) => FileInputStream (Đọc dữ liệu từ File ra) => DeflaterInputStream (Nén dữ liệu từ đầu đọc) => Dữ liệu đầu ra sẽ được nén => save xuống File (FileOutputStream) => File (Zip)
Thread 2>> File (Source) => FileInputStream (Đọc) => Dữ liệu thường ko bị nén => DeflaterOutputStream => Nén dữ liệu=> FileOutStream (ghi file) => Save File (nén)
Loại 2-2: Giải nén File
File (zip) => File (text) giải nén
Thread 1 >> File (zip) => FileInputStream (Đọc dữ liệu nén) => InflaterInputStream (Giải nén lúc đọc dữ liệu) => dữ liệu text thường => save (FileOutStream) => File (text)
Thread 2 >> File (zip) => FileInputStream (Đọc dữ liệu nén) => Dữ liệu nén => InflaterOutputStream (Giải nén lúc lưu dữ liệu) => Save (FileOutputStream) => File (text)
#DeflaterTest.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 lession4;
import java.io.UnsupportedEncodingException;
import java.util.zip.Deflater;
/**
*
* @author Diep.Tran
*/
public class DeflaterTest {
public static void main(String[] args) throws UnsupportedEncodingException {
//Chuoi can nen
String str = "Học cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7Zip";
//Truoc khi nen => can chuyen String to mang bytes
//Tai sao lai nhu vay => Giai thuat no quy dinh the.
byte[] inputData = str.getBytes("utf-8");
byte[] outData = new byte[1000];
//Khai bao 1 doi tuong nen (Giai thuat nen)
Deflater deflater = new Deflater();
//Chuyen du lieu can nen vao
deflater.setInput(inputData);
//Ket thuc cai dat du lieu can nen
deflater.finish();
//thuc hien nen du lieu
//outData => chua du lieu dau ra
//compressLength => Tra ve kich thuoc sau khi dc nen
int compressLength = deflater.deflate(outData);
System.out.println("compressLength : " + compressLength);
System.out.println("Length: " + inputData.length);
}
}
#FileDelaterInputTest.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 lession4;
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;
/**
*
* @author Diep.Tran
*/
public class FileDelaterInputTest {
public static void main(String[] args) {
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) {
//code => du lieu da dc nen
//save du lieu dc nen xuong file data.zip
fos.write(code);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(FileDelaterInputTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(FileDelaterInputTest.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(fis != null)
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(FileDelaterInputTest.class.getName()).log(Level.SEVERE, null, ex);
}
if(dis != null) {
try {
dis.close();
} catch (IOException ex) {
Logger.getLogger(FileDelaterInputTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(FileDelaterInputTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
#FileDelaterOutputTest.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 lession4;
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.DeflaterOutputStream;
/**
*
* @author Diep.Tran
*/
public class FileDelaterOutputTest {
public static void main(String[] args) {
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) {
//code => du lieu da dc nen
//save du lieu dc nen xuong file data.zip
dos.write(code);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(FileDelaterInputTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(FileDelaterInputTest.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(fis != null)
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(FileDelaterInputTest.class.getName()).log(Level.SEVERE, null, ex);
}
if(dos != null) {
try {
dos.close();
} catch (IOException ex) {
Logger.getLogger(FileDelaterInputTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(FileDelaterInputTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
#FileInflaterInputTest.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 lession4;
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.InflaterInputStream;
/**
*
* @author Diep.Tran
*/
public class FileInflaterInputTest {
public static void main(String[] args) {
FileInputStream fis = null;
InflaterInputStream iis = null;
FileOutputStream ios = null;
try {
fis = new FileInputStream("data.zip");
iis = new InflaterInputStream(fis);
ios = new FileOutputStream("data-inf.txt");
int code;
while((code = iis.read()) != -1) {
//code => du lieu da dc giai nen
//save du lieu giai nen vao file
ios.write(code);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(FileInflaterInputTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(FileInflaterInputTest.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(fis != null) {
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(FileInflaterInputTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(iis != null) {
try {
iis.close();
} catch (IOException ex) {
Logger.getLogger(FileInflaterInputTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(ios != null) {
try {
ios.close();
} catch (IOException ex) {
Logger.getLogger(FileInflaterInputTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
#FileInflaterOutputTest.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 lession4;
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.InflaterInputStream;
import java.util.zip.InflaterOutputStream;
/**
*
* @author Diep.Tran
*/
public class FileInflaterOutputTest {
public static void main(String[] args) {
FileInputStream fis = null;
InflaterOutputStream ios = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("data.zip");
fos = new FileOutputStream("data-inf2.txt");
ios = new InflaterOutputStream(fos);
int code;
while((code = fis.read()) != -1) {
//code => du lieu nen
ios.write(code);//nen sau do moi luu
}
} catch (FileNotFoundException ex) {
Logger.getLogger(FileInflaterInputTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(FileInflaterInputTest.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(fis != null) {
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(FileInflaterInputTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(FileInflaterInputTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(ios != null) {
try {
ios.close();
} catch (IOException ex) {
Logger.getLogger(FileInflaterInputTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
#InflaterTest.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 lession4;
import java.io.UnsupportedEncodingException;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
/**
*
* @author Diep.Tran
*/
public class InflaterTest {
public static void main(String[] args) throws UnsupportedEncodingException, DataFormatException {
//Fake data nen
String str = "Gokisoft - Học cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7ZipHọc cách xây dựng 1 project => WinRar, WinZip, 7Zip";
byte[] inputData = str.getBytes("utf-8");
byte[] outData = new byte[1000];
Deflater deflater = new Deflater();
deflater.setInput(inputData);
deflater.finish();
int compressLength = deflater.deflate(outData);
//code chuc nan giai nen
byte[] outDataInflater = new byte[1000];
Inflater inflater = new Inflater();
inflater.setInput(outData, 0, compressLength);
int length = inflater.inflate(outDataInflater);
String strOutputInflater = new String(outDataInflater, 0, length);
System.out.println(strOutputInflater);
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)