java压缩流的用法_Java对压缩包的操作(解压缩)
image
前言
如何用Java對文件進行加壓和壓縮
上篇文章說了項目中對根據URL提供的HTML代碼中的文件URL進行下載,將下載后的文件存放在服務器上,但是文件下載下來都是ZIP壓縮包。那么這篇就來看Java如何多文件進行解壓縮操作。
一、正文
這里沒有使用其他的jar包,利用Java中的IO流直接對文件進行操作,為了方便將文件放入桌面,路徑為:C:\Users\Surpass\Desktop。
二、使用步驟
博主盡量在代碼中添加明確的注釋,以便于理解,所以直接貼代碼了。
1.單文件壓縮
/**
* @author Surpass
* @Package com.hkrt.demo.zip
* @Description: 單文件壓縮
* @date 2020/10/16 10:51
*/
public class SingleZipCompression {
private static InputStream inputStream;
private static ZipOutputStream zipOutputStream;
private static OutputStream outputStream;
private static String filePath = "C:\\Users\\Surpass\\Desktop\\Linux就該這么學 高清晰PDF.pdf";
public static void main(String[] args) {
try {
//文件輸入流
File file = new File(filePath);
inputStream = new FileInputStream(file);
//壓縮輸出路徑的流 壓縮文件路徑+壓縮文件名前綴(Linux就該這么學 高清晰PDF)+.zip
outputStream = new FileOutputStream(file.getParent()+"\\"+file.getName().substring(0,file.getName().lastIndexOf("."))+".zip");
zipOutputStream = new ZipOutputStream(outputStream);
//壓縮包內文件的名字 Linux就該這么學 高清晰PDF.pdf
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
//輸出文件
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes))!=-1){
zipOutputStream.write(bytes,0,len);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (zipOutputStream!=null){
zipOutputStream.closeEntry();
zipOutputStream.close();
}
if (outputStream!=null){
outputStream.close();
}
if (inputStream!=null){
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.單文件解壓
/**
* @author Surpass
* @Package com.hkrt.demo.zip
* @Description: 壓縮包解壓
* @date 2020/10/16 10:50
*/
public class SingleZipUnpackThe {
private static InputStream inputStream;
private static ZipInputStream bi;
private static OutputStream fileOutputStream;
private static String zipPath = "C:\\Users\\Surpass\\Desktop\\Bypass_1.14.2\\Bypass\\使用須知.zip";
//方法二
public static void main(String[] args) {
try {
//對中文名字進行了處理
ZipFile zipFile = new ZipFile(zipPath, Charset.forName("GBK"));
String zipFileParentPath = zipFile.getName().substring(0,zipFile.getName().lastIndexOf("\\"));
System.out.println(zipFileParentPath);
//獲得壓縮包內文件
Enumeration extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry zipEntry = entries.nextElement();
//輸出文件流 壓縮包路徑+文件
fileOutputStream = new FileOutputStream(zipFileParentPath+"\\" + zipEntry.getName());
//寫文件
byte[] bytes = new byte[1024];
int len = 0;
while ((len = bi.read(bytes))!=-1){
fileOutputStream.write(bytes,0,len);
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (bi!=null){
bi.closeEntry();
bi.close();
}
if (fileOutputStream!=null){
fileOutputStream.close();
}
if (inputStream!=null){
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.多文件壓縮(保留原有結構)
/**
* @author Surpass
* @Package com.hkrt.demo.zip
* @Description: 多文件根據目錄結構壓縮
* @date 2020/10/17 10:13
*/
public class MultipleFilesCompression {
private static InputStream inputStream;
private static ZipOutputStream zipOutputStream;
private static OutputStream outputStream;
private static String filePath = "C:\\Users\\Surpass\\Desktop\\Bypass_1.14.25";
public static void main(String[] args) {
try {
//需要壓縮的文件夾
File file = new File(filePath);
String dirName = file.getName();
String fileParentPath = file.getParent();
//需要生成的壓縮包名稱和生成路徑
outputStream = new FileOutputStream(fileParentPath+"\\"+dirName+".zip");
zipOutputStream = new ZipOutputStream(outputStream);
//獲取目錄結構
Map map = new HashMap<>();
map = getFile(file, map);
//通過key遍歷map
Set keySet = map.keySet();
Iterator iterator = keySet.iterator();
while (iterator.hasNext()){
//key(當是空文件夾的時候key為目錄,當文件夾有文件的時候key為文件名)
String fileName = iterator.next();
//value(當是空文件夾的時候value為"",當文件夾有文件的時候value為目錄)
String path = map.get(fileName);
if (path.equals("")){
//空文件夾
//這里獲取從壓縮包開始的路徑 \Bypass\Logs>>>>>>2020-09-12.txt \Bypass\Music
String[] basePath = fileName.split(dirName);
String parent = basePath[1];
//壓入壓縮包流文件的存放路徑 \Bypass\Music
zipOutputStream.putNextEntry(new ZipEntry(parent+"/"));
}else {
//正常文件
//文件轉輸入流
inputStream = new FileInputStream(path+"\\"+fileName);
//這里獲取從壓縮包開始的路徑 \Bypass\Logs>>>>>>2020-09-12.txt \Bypass>>>>>>使用須知.txt
String[] basePath = path.split(dirName);
String parent = basePath[1];
zipOutputStream.putNextEntry(new ZipEntry(parent +"\\"+fileName));
}
//寫文件
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes))!=-1){
zipOutputStream.write(bytes,0,len);
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//關閉
try {
if (zipOutputStream!=null){
zipOutputStream.closeEntry();
zipOutputStream.close();
}
if (outputStream!=null){
outputStream.close();
}
if (inputStream!=null){
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @Description: 使用遞歸的方式向map中存入目錄結構
* @param file, 需要壓縮的文件
* map 存放目錄結構
* @return java.util.Map
* @throws
* @author Surpass
* @date 2020/10/17 11:26
*/
private static Map getFile(File file,Map map){
File[] files = file.listFiles();
//如果是空文件夾的時候使用路徑作為key
if (files.length==0){
map.put(file.getAbsolutePath(),"");
}
for (File file1 : files) {
if (file1.isDirectory()){
//遞歸
getFile(file1,map);
}
if (file1.isFile()){
//文件作為key,路徑作為value
map.put(file1.getName(),file1.getParent());
}
}
return map;
}
}
4.多文件解壓(保留原有結構)
/**
* @author Surpass
* @Package com.hkrt.demo.zip
* @Description: 壓縮包解壓保持原有的目錄
* @date 2020/10/17 11:39
*/
public class MultipleFilesUnpackThe {
private static OutputStream outputStream;
private static InputStream inputStream;
private static ZipInputStream bi;
private static String ZipPath = "C:\\Users\\Surpass\\Desktop\\Bypass.zip";
public static void main(String[] args) {
try {
//對中文名字進行了處理
ZipFile zipFile = new ZipFile(ZipPath, Charset.forName("GBK"));
//壓縮包的名字,不包含后綴 .zip
String zipName = zipFile.getName().substring(0, zipFile.getName().indexOf("."));
//壓縮包所在的路徑
String zipFileParentPath = zipFile.getName().substring(0,zipFile.getName().lastIndexOf("\\"));
Enumeration extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (entry.isDirectory()) {
//空文件夾,直接創建 壓縮包路徑+壓縮包名字+空文件夾路徑
File file = new File(zipFileParentPath+"\\"+zipName+"\\"+entry);
file.mkdirs();
}else {
//獲取文件在壓縮包內的路徑
String entryPath = entry.getName().substring(0,entry.getName().lastIndexOf("\\"));
//為存放的路徑創建文件夾
File file = new File(zipFileParentPath+"\\"+zipName+"\\"+entryPath);
if (!file.exists()) {
file.mkdirs();
}
outputStream = new FileOutputStream(zipFileParentPath+"\\"+zipName+"\\"+ entry.getName());
}
//寫文件
byte[] bytes = new byte[1024];
int len = 0;
while ((len = bi.read(bytes))!=-1){
outputStream.write(bytes,0,len);
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (bi!=null){
bi.closeEntry();
bi.close();
}
if (inputStream!=null){
inputStream.close();
}
if (outputStream!=null){
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
總結
看網上的例子說有什么jar能簡單一下,博主這里沒有嘗試去看,既然這樣寫了,也算是一個不錯的練習。當然,博主道行較淺,代碼不規范是一方面,如果有什么不足之處,還望各位大牛批評指正。
總結
以上是生活随笔為你收集整理的java压缩流的用法_Java对压缩包的操作(解压缩)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 左右布艺沙发报价详情
- 下一篇: java集合框架介绍_Java集合框架介