By GokiSoft.com|
10:07 16/08/2021|
Java Advanced
[Share Code] Zip File - UnZip File trong Java - Viết chương nén và giải nén File - Lập trình Java nâng cao
I) ZIP
1. Data Source (data-web.txt) -> Doc du lieu (FileInputStream) -> data -> -> Nen (DeflaterOutputStream) -> Luu file (FileOutputStream) -> Des Data (data-web.dlf)
2. Data Source (data-web.txt) -> Doc du lieu (FileInputStream) -> Nen -> data -> Luu file (FileOutputStream) -> Des Data (data-web.dlf)
II) UpZip
1. Data Source (data-web.dlf) -> Doc du lieu (FileInputStream) -> Giai nen (InflaterInputStream) -> data tho -> Luu (FileOutputStream) -> original data
2. Data Source (data-web.dlf) -> Doc du lieu (FileInputStream) -> data nen -> Giai nen (InflaterOutputStream) -> Luu (FileOutputStream) -> original data
#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) {
//zipText();
//zipFile();
//zipFile2();
upzipFile();
}
static void upzipFile() {
FileInputStream fis = null;
InflaterInputStream iis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("data-web.dfl");
iis = new InflaterInputStream(fis);
fos = new FileOutputStream("original-data-web.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 zipFile2() {
FileInputStream fis = null;
FileOutputStream fos = null;
DeflaterOutputStream dos = null;
try {
fis = new FileInputStream("data-web.txt");
fos = new FileOutputStream("data-web2.dfl");
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 zipFile() {
FileInputStream fis = null;
DeflaterInputStream dis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("data-web.txt");
dis = new DeflaterInputStream(fis);
fos = new FileOutputStream("data-web.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);
}
}
}
}
static void zipText() {
try {
//Nen du lieu
String str = "Tạo một lớp đối tượng book gồm các thuộc tính : tên sách, tác giả, giá bán, ngày xuất bản, nhà sản xuất.Tạo một lớp đối tượng book gồm các thuộc tính : tên sách, tác giả, giá bán, ngày xuất bản, nhà sản xuất.Tạo một lớp đối tượng book gồm các thuộc tính : tên sách, tác giả, giá bán, ngày xuất bản, nhà sản xuất.";
System.out.println("Original Length: " + str.length());
byte[] originalData = str.getBytes("utf8");
Deflater deflater = new Deflater();
deflater.setInput(originalData);
deflater.finish();
byte[] zipData = new byte[1000];
int length = deflater.deflate(zipData);
System.out.println("length: " + length);
//String zipTxt = new String(zipData, 0, length);
//System.out.println(zipTxt);
//Giai nen du lieu
Inflater inflater = new Inflater();
inflater.setInput(zipData, 0, length);
byte[] unzipData = new byte[1000];
int unzipLength = inflater.inflate(unzipData);
System.out.println("unzip length: " + unzipLength);
String unzipTxt = new String(unzipData, 0, unzipLength);
System.out.println(unzipTxt);
} 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)