麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發(fā) > Java > 正文

Java壓縮解壓縮工具類

2024-07-14 08:43:13
字體:
供稿:網(wǎng)友

本文實例為大家分享了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 :壓縮后文件的名稱(不包括擴展名)   * @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實體,并添加進壓縮包        ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());        zos.putNextEntry(zipEntry);        //讀取待壓縮的文件并寫進壓縮包里        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;  }   /** * 壓縮單個文件 * @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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到JAVA教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 欧美黑大粗硬毛片视频 | 成人在线视频在线观看 | 毛片在线视频观看 | 99热高清 | 黑人三级毛片 | 国产一级性生活视频 | 国产免费片 | 狠狠干91 | 国产精品99久久久久久宅女 | 亚洲5区 | 午夜视频在线免费播放 | 亚洲成人福利在线观看 | 热re91久久精品国产99热 | 黄片毛片一级 | 日日碰日日操 | 成人午夜精品久久久久久久蜜臀 | 欧美人与牲禽动交精品一区 | 精品久久久久久亚洲精品 | 男女视频免费看 | 久久免费视频精品 | 久久精品亚洲精品国产欧美kt∨ | 爱视频福利 | 国产一区精品视频 | 精品国产一区二区三区在线观看 | 天堂福利电影 | 久久久久久久久久久一区 | 毛片大全在线观看 | 日本一级黄色大片 | 欧美日韩国产成人在线 | 热@国产 | 精品亚洲视频在线 | 蜜桃网站在线观看 | 国产精品亚洲yourport | 中文字幕精品在线视频 | 日韩精品中文字幕一区二区三区 | 亚洲一区二区三区四区精品 | 男男羞羞视频网站国产 | 视频一区二区三区免费观看 | 国产一区影院 | 日韩毛片免费观看 | 91精品国产综合久久青草 |