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

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

java zip壓縮優(yōu)化版 解決壓縮后文件一直被占用無法刪除

2019-11-14 21:11:53
字體:
供稿:網(wǎng)友
java zip壓縮優(yōu)化版 解決壓縮后文件一直被占用無法刪除

最近進(jìn)行zip操作,從網(wǎng)上找到一個(gè)處理方法,但是經(jīng)過試驗(yàn)存在一些bug,主要是文件流的申明存在問題,導(dǎo)致jvm一直占用文件而不釋放,特意把自己修改的發(fā)出來,已備記錄

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.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;public class Zippic {    public final static class FileToZip {        PRivate FileToZip() {        }        /**         * 將存放在sourceFilePath目錄下的源文件,打包成fileName名稱的ZIP文件,并存放到zipFilePath。          * @param sourceFilePath 待壓縮的文件路徑          * @param zipFilePath 壓縮后存放路徑          * @param fileName 壓縮后文件的名稱          * @return flag         */        public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName) {            boolean flag = false;            File sourceFile = new File(sourceFilePath);            if(sourceFile.exists() == false) {                System.out.println(">>>>>> 待壓縮的文件目錄:" + sourceFilePath + " 不存在. <<<<<<");                flag = false;                return flag;            } else {                try {                    File zipFile = new File(zipFilePath + "/" + fileName + ".zip");                    if(zipFile.exists()) {                        System.out.println(">>>>>> " + zipFilePath + " 目錄下存在名字為:" + fileName + ".zip" + " 打包文件. <<<<<<");                    } else {                        File[] sourceFiles = sourceFile.listFiles();                        if(null == sourceFiles || sourceFiles.length < 1) {                            System.out.println(">>>>>> 待壓縮的文件目錄:" + sourceFilePath + " 里面不存在文件,無需壓縮. <<<<<<");                            flag = false;                            return flag;                        } else {                            ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));                    //用到時(shí)才申明,否則容易出現(xiàn)問題,記得先開后關(guān),后開先關(guān)                            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)壓縮包里                                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFiles[i]),1024*10);                    //用到時(shí)才申明,否則容易出現(xiàn)問題,記得先開后關(guān),后開先關(guān)                                int read = 0;                                while((read=(bis.read(bufs, 0, 1024*10))) != -1) {                                    zos.write(bufs, 0, read);                                }                                if(null != bis) bis.close();  //關(guān)閉                            }                            flag = true;                            if(null != zos) zos.close();  //關(guān)閉                        }                    }                } catch (FileNotFoundException e) {                    e.printStackTrace();                    throw new RuntimeException(e);                } catch (IOException e) {                    e.printStackTrace();                    throw new RuntimeException(e);                }            }            return flag;        }    }}

  


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宾阳县| 沈阳市| 惠水县| 营山县| 鄯善县| 黄浦区| 舟山市| 荥经县| 巴南区| 扎囊县| 将乐县| 江阴市| 富川| 慈利县| 大荔县| 青铜峡市| 丘北县| 汤阴县| 团风县| 醴陵市| 石狮市| 山阴县| 西乌| 甘谷县| 泸溪县| 平乡县| 石渠县| 龙岩市| 新竹市| 大悟县| 游戏| 随州市| 惠水县| 措勤县| 湟中县| 柳林县| 靖远县| 前郭尔| 常德市| 车险| 江口县|