您好,登錄后才能下訂單哦!
需要了解的知識(shí)匯總:
DeflaterOutputStream :壓縮類的基類。
ZipOutputStream :DeflaterOutputStream的一個(gè)子類,把數(shù)據(jù)壓縮成Zip文件格式
GZIPOutputStream :DeflaterOutputStream的一個(gè)子類,把數(shù)據(jù)壓縮成GZip文件格式
InflaterInputStream :解壓縮類的基類
ZipInputStream :InflaterInputStream的一個(gè)子類,能解壓縮Zip格式的數(shù)據(jù)
GZIPInputStream :InflaterInputStream的一個(gè)子類,能解壓縮Zip格式的數(shù)據(jù)
ZipEntry 類:表示 ZIP 文件條目
ZipFile 類:此類用于從 ZIP 文件讀取條目
以zipentry來命名zip內(nèi)文件列表。
以FileInputStream來讀取文件。
以zipoutputstream輸出流進(jìn)行壓縮文件寫入。
邏輯簡單好懂,附件帶有實(shí)例,解壓即可運(yùn)行。
百度網(wǎng)盤實(shí)例鏈接:https://pan.baidu.com/s/1VSs-UqtU_3Lld2H7-1dMMg
提取碼:mcb3
package com.crscd.server.web.cbtc2.filezip;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
public class FileZip {
/*
author:zhao_anan 20190424 zip compress program
/
/public void zip(ZipOutputStream out, File f, String base, boolean first)
throws Exception {
if (first) {
if (f.isDirectory()) {
out.putNextEntry(new ZipEntry("/"));
base = base + f.getName();
first = false;
} else
base = f.getName();
}
if (f.isDirectory()) {
File[] fl = f.listFiles();
base = base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName(), first);
}
} else {
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
System.out.println(base);
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
}
}*/
/*
unzip
/
/public void unZipFileByOpache(ZipFile zipFile, String unZipRoot)
throws Exception, IOException {
Enumeration<?> e = zipFile.entries();
ZipEntry zipEntry;
while (e.hasMoreElements()) {
zipEntry = (ZipEntry) e.nextElement();
InputStream fis = zipFile.getInputStream(zipEntry);
if (zipEntry.isDirectory()) {
} else {
File file = new File(unZipRoot + File.separator
public void ZipFile(String zipFileName, String inputFileName)
throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipFileName));
File inputFile = new File(inputFileName);
zip(out, inputFile, "", true);
System.out.println("zip done");
out.close();
}
public void unZipFile(String unZipFileName, String unZipPath)
throws Exception {
//ZipFile zipFile = new ZipFile(unZipFileName);
//unZipFileByOpache(zipFile, unZipPath);
System.out.println("unZip Ok");
}
public static void main(String[] args) throws Exception {
FileZip fileZip = new FileZip();
// ZipFile("d:/testfile.zip","d:/TTPlayer");
fileZip.unZipFile("d:/20120206_SLIS_0002_DY2.zip", "d:/");
}*/
/**
*/
public void unZipFile(String zipFilename, String unzipRoot)
throws Exception {
BufferedOutputStream bos = null;
FileInputStream fis = new FileInputStream(zipFilename);
BufferedInputStream bis = new BufferedInputStream(fis);
ZipInputStream zis = new ZipInputStream(bis);
ZipEntry entry = null;
int count = 0;
byte buf[] = new byte[1024];
while ((entry = zis.getNextEntry()) != null) {
if (entry.isDirectory()) {
} else {
File file = new File(unzipRoot + File.separator
+ entry.getName());
File parentFile = file.getParentFile();
parentFile.mkdirs();
bos = new BufferedOutputStream(new FileOutputStream(file));
while ((count = zis.read(buf)) != -1) {
bos.write(buf, 0, count);
}
bos.flush();
bos.close();
}
}
zis.close();
}
/**
*/
private void zip(ZipOutputStream out, File f, String base, boolean first)
throws Exception {
if (first) {
if (f.isDirectory()) {
out.putNextEntry(new ZipEntry("/"));
base = base + f.getName();
first = false;
} else
base = f.getName();
}
if (f.isDirectory()) {
File[] fl = f.listFiles();
base = base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName(), first);
}
} else {
out.putNextEntry(new ZipEntry(base));
// FileInputStream in = new FileInputStream(f);
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(f), "ISO8859_1"));
int b;
System.out.println(base);
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
}
}
public void zipFile(String zipFileName, String inputFileName)
throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipFileName));
File inputFile = new File(inputFileName);
zip(out, inputFile, "", true);
System.out.println("zip done");
out.close();
}
/**
*/
private void zipList(ZipOutputStream out, String[] fileList, String base)
throws Exception {
base = base + "\";
for (int i = 0; i < fileList.length; i++) {
//zip(out, , base + fileList[i], first);
out.putNextEntry(new ZipEntry(fileList[i]));
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(base + fileList[i]), "ISO8859_1"));
int b;
System.out.println(base + fileList[i]);
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
}
}
public void zipListFile(String zipFileName, String inputListFiles, String baseurl)
throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipFileName));
String[] FileList = inputListFiles.split(",");
zipList(out, FileList, baseurl);
System.out.println("zip done");
out.close();
}
public static void main(String[] args) {
//壓縮測(cè)試
FileZip tFileZip = new FileZip();
String tinputFileName = "E:\\temp";
String baseurl = "E:\\temp";
String tzipFileName = "E:\\temp.zip";
String inputListFiles = "application-dev.yml,auto_arrange.yml";
try {
//tFileZip.zipFile(tzipFileName, tinputFileName);
tFileZip.zipListFile(tzipFileName, inputListFiles, baseurl);
//tFileZip.unZipFile(tzipFileName, baseurl + "unzip");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。