本文實(shí)例為大家分享了Java壓縮解壓縮工具類的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)建站專注于廣昌網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供廣昌營銷型網(wǎng)站建設(shè),廣昌網(wǎng)站制作、廣昌網(wǎng)頁設(shè)計(jì)、廣昌網(wǎng)站官網(wǎng)定制、小程序設(shè)計(jì)服務(wù),打造廣昌網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供廣昌網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
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(); } } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
文章題目:Java壓縮解壓縮工具類
網(wǎng)站鏈接:http://aaarwkj.com/article0/iihoio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、網(wǎng)站改版、、微信小程序、關(guān)鍵詞優(yōu)化、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)