Java壓縮解壓縮工具類
更新時(shí)間:2018年12月15日 15:59:38 作者:一掬凈土
這篇文章主要為大家詳細(xì)介紹了Java壓縮解壓縮工具類,如何壓縮單個(gè)文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了Java壓縮解壓縮工具類的具體代碼,供大家參考,具體內(nèi)容如下
package com.wdy.tools.utils.pressuitl;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.apache.commons.logging.Log;
import com.wdy.tools.utils.LogUtil;
/**
* 壓縮/解壓縮工具類(zip格式)
*
* @author wdy
* @date 2016-08-23
*/
public class PressUtil {
private static final Log log = LogUtil.getLog(PressUtil.class);
public static void main(String[] args) {
// PressUtil.ZipMultiFile("d:\\nwp_trans\\nwp_h\\", "d:\\nwp_trans\\nwp_h\\wdy.zip");
String sourceFilePath = "d:\\nwp_trans\\nwp_h\\";
String zipFilePath = "d:\\nwp_trans\\nwp_h\\";
String fileName = "wdy";
boolean flag = PressUtil.fileToZip(sourceFilePath, zipFilePath, fileName);
if(flag){
log.info("文件打包成功!");
}else{
log.info("文件打包失敗!");
}
}
/**
* 將存放在sourceFilePath目錄下的源文件,打包成fileName名稱的zip文件,并存放到zipFilePath路徑下
* @param sourceFilePath :待壓縮的文件路徑
* @param zipFilePath :壓縮后存放路徑
* @param fileName :壓縮后文件的名稱(不包括擴(kuò)展名)
* @return
*/
@SuppressWarnings("resource")
public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName){
boolean flag = false;
File sourceFile = new File(sourceFilePath);
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
if(sourceFile.exists() == false){
log.info("待壓縮的文件目錄:"+sourceFilePath+"不存在.");
}else{
try {
File zipFile = new File(zipFilePath + "/" + fileName +".zip");
if(zipFile.exists()){
log.info(zipFilePath + "目錄下存在名字為:" + fileName +".zip" +"打包文件.");
}else{
File[] sourceFiles = sourceFile.listFiles();
if(null == sourceFiles || sourceFiles.length<1){
log.info("待壓縮的文件目錄:" + sourceFilePath + "里面不存在文件,無需壓縮.");
}else{
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
byte[] bufs = new byte[1024*10];
for(int i=0;i<sourceFiles.length;i++){
//創(chuàng)建ZIP實(shí)體,并添加進(jìn)壓縮包
ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
zos.putNextEntry(zipEntry);
//讀取待壓縮的文件并寫進(jìn)壓縮包里
fis = new FileInputStream(sourceFiles[i]);
bis = new BufferedInputStream(fis, 1024*10);
int read = 0;
while((read=bis.read(bufs, 0, 1024*10)) != -1){
zos.write(bufs,0,read);
}
}
flag = true;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally{
//關(guān)閉流
try {
if(null != bis) bis.close();
if(null != zos) zos.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
return flag;
}
/**
* 壓縮單個(gè)文件
* @param filePath 要被壓縮的文件的全路徑,包含文件名d:/hello.txt
* @param zipPath 壓縮后的全路徑,包含文件名d:/hello.zip
*/
public static void zipOneFile(String filePath, String zipPath) {
try {
File file = new File(filePath);
File zipFile = new File(zipPath);
InputStream input = new FileInputStream(file);
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
zipOut.putNextEntry(new ZipEntry(file.getName()));
int temp = 0;
while ((temp = input.read()) != -1) {
zipOut.write(temp);
}
input.close();
zipOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- java 壓縮和解壓縮Zip、Jar、Gzip文件實(shí)例代碼
- Java8 Zip 壓縮與解壓縮的實(shí)現(xiàn)
- Java GZIP壓縮與解壓縮代碼實(shí)例
- java實(shí)現(xiàn)哈夫曼壓縮與解壓縮的方法
- Java實(shí)現(xiàn)的zip壓縮及解壓縮工具類示例
- java ant包中的org.apache.tools.zip實(shí)現(xiàn)壓縮和解壓縮實(shí)例詳解
- Java解壓縮zip - 解壓縮多個(gè)文件或文件夾實(shí)例
- 實(shí)例展示使用Java壓縮和解壓縮7z文件的方法
- 通過java api實(shí)現(xiàn)解壓縮zip示例
- 利用Java實(shí)現(xiàn)zip壓縮/解壓縮
- 用Java進(jìn)行zip文件壓縮與解壓縮
相關(guān)文章
Mybatis Plus整合PageHelper分頁的實(shí)現(xiàn)示例
這篇文章主要介紹了Mybatis Plus整合PageHelper分頁的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
SpringSecurity之SecurityContextHolder使用解讀
這篇文章主要介紹了SpringSecurity之SecurityContextHolder使用解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Spring Boot 整合 Druid 并開啟監(jiān)控的操作方法
本文介紹了如何在SpringBoot項(xiàng)目中引入和配置Druid數(shù)據(jù)庫連接池,并開啟其監(jiān)控功能,通過添加依賴、配置數(shù)據(jù)源、開啟監(jiān)控、自定義配置以及訪問監(jiān)控頁面,開發(fā)者可以有效提高數(shù)據(jù)庫訪問效率并監(jiān)控連接池狀態(tài),感興趣的朋友跟隨小編一起看看吧2025-01-01
JavaSwing FlowLayout 流式布局的實(shí)現(xiàn)
這篇文章主要介紹了JavaSwing FlowLayout 流式布局的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12

