国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Java壓縮解壓縮工具類

2024-07-14 08:43:13
字體:
來源:轉載
供稿:網友

本文實例為大家分享了Java壓縮解壓縮工具類的具體代碼,供大家參考,具體內容如下

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++){        //創建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{     //關閉流     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(); } }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 山阳县| 富源县| 青河县| 怀来县| 桑植县| 阳曲县| 临泽县| 东阿县| 凉山| 安福县| 新竹市| 赤水市| 墨玉县| 玉山县| 瑞金市| 新安县| 荆州市| 青岛市| 黄大仙区| 滦平县| 霍山县| 阿勒泰市| 桐乡市| 张家界市| 长泰县| 卫辉市| 岳阳县| 和田县| 阜康市| 长阳| 平定县| 盘锦市| 蛟河市| 太保市| 麻江县| 微山县| 沁水县| 永定县| 庆云县| 株洲县| 资阳市|