壓縮解壓CompressUtils

import com.google.common.collect.Lists;
import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.util.Enumeration;
import java.util.List;

/**
 * <p>User: Zhang Kaitao
 * <p>Date: 13-6-14 下午3:37
 * <p>Version: 1.0
 */
public class CompressUtils {

    public static final void zip(String compressPath, String[] needCompressPaths) {
        File compressFile = new File(compressPath);

        List<File> files = Lists.newArrayList();
        for (String needCompressPath : needCompressPaths) {
            File needCompressFile = new File(needCompressPath);
            if (!needCompressFile.exists()) {
                continue;
            }
            files.add(needCompressFile);
        }
        try {
            ZipArchiveOutputStream zaos = null;
            try {
                zaos = new ZipArchiveOutputStream(compressFile);
                zaos.setUseZip64(Zip64Mode.AsNeeded);
                zaos.setEncoding("GBK");

                for (File file : files) {
                    addFilesToCompression(zaos, file, "");
                }

            } catch (IOException e) {
                throw e;
            } finally {
                IOUtils.closeQuietly(zaos);
            }
        } catch (Exception e) {
            FileUtils.deleteQuietly(compressFile);
            throw new RuntimeException("壓縮失敗", e);
        }
    }

    private static void addFilesToCompression(ZipArchiveOutputStream zaos, File file, String dir) throws IOException {

        ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, dir + file.getName());
        zaos.putArchiveEntry(zipArchiveEntry);

        if (file.isFile()) {
            BufferedInputStream bis = null;
            try {
                bis = new BufferedInputStream(new FileInputStream(file));
                IOUtils.copy(bis, zaos);
                zaos.closeArchiveEntry();
            } catch (IOException e) {
                throw e;
            } finally {
                IOUtils.closeQuietly(bis);
            }
        } else if (file.isDirectory()) {
            zaos.closeArchiveEntry();

            for (File childFile : file.listFiles()) {
                addFilesToCompression(zaos, childFile, dir + file.getName() + File.separator);
            }
        }
    }

    public static void unzip(String path, String descPath, boolean override) {
        File uncompressFile = new File(path);
        File descPathFile = null;

        try {
            descPathFile = new File(descPath);
            unzipFolder(uncompressFile, descPathFile, override);
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    private static void unzipFolder(File uncompressFile, File descPathFile, boolean override) {

        ZipFile zipFile = null;
        try {
            zipFile = new ZipFile(uncompressFile, "GBK");

            Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
            while (entries.hasMoreElements()) {
                ZipArchiveEntry zipEntry = entries.nextElement();
                String name = zipEntry.getName();
                name = name.replace("\\", "/");

                File currentFile = new File(descPathFile, name);

                //非覆蓋 跳過
                if(currentFile.isFile() && currentFile.exists() && !override) {
                    continue;
                }

                if(name.endsWith("/")) {
                    currentFile.mkdirs();
                    continue;
                } else {
                    currentFile.getParentFile().mkdirs();
                }

                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(currentFile);
                    InputStream is = zipFile.getInputStream(zipEntry);
                    IOUtils.copy(is, fos);
                } finally {
                    IOUtils.closeQuietly(fos);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException("解壓縮失敗", e);
        } finally {
            if (zipFile != null) {
                try {
                    zipFile.close();
                } catch (IOException e) {
                }
            }
        }

    }


    public static void main(String[] args) {
       String[] needCompress = new String[]{
                "E:/test/abc",
                "E:/test/after.pdf",
                "E:/test/test.txt"
        };

        zip("E:/testZip.zip", needCompress);

        unzip("E:/testZip.zip", "E:\\新建文件夾",true);
    }
}

代碼來源

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末倚搬,一起剝皮案震驚了整個(gè)濱河市拗盒,隨后出現(xiàn)的幾起案子毫蚓,更是在濱河造成了極大的恐慌碌识,老刑警劉巖锻离,帶你破解...
    沈念sama閱讀 216,744評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件穷躁,死亡現(xiàn)場離奇詭異仇让,居然都是意外死亡那槽,警方通過查閱死者的電腦和手機(jī)蜈膨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評論 3 392
  • 文/潘曉璐 我一進(jìn)店門屿笼,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人翁巍,你說我怎么就攤上這事驴一。” “怎么了灶壶?”我有些...
    開封第一講書人閱讀 163,105評論 0 353
  • 文/不壞的土叔 我叫張陵肝断,是天一觀的道長。 經(jīng)常有香客問我驰凛,道長胸懈,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,242評論 1 292
  • 正文 為了忘掉前任恰响,我火速辦了婚禮箫荡,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘渔隶。我一直安慰自己羔挡,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,269評論 6 389
  • 文/花漫 我一把揭開白布间唉。 她就那樣靜靜地躺著绞灼,像睡著了一般。 火紅的嫁衣襯著肌膚如雪呈野。 梳的紋絲不亂的頭發(fā)上低矮,一...
    開封第一講書人閱讀 51,215評論 1 299
  • 那天,我揣著相機(jī)與錄音被冒,去河邊找鬼军掂。 笑死轮蜕,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的蝗锥。 我是一名探鬼主播跃洛,決...
    沈念sama閱讀 40,096評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼终议!你這毒婦竟也來了汇竭?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,939評論 0 274
  • 序言:老撾萬榮一對情侶失蹤穴张,失蹤者是張志新(化名)和其女友劉穎细燎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體皂甘,經(jīng)...
    沈念sama閱讀 45,354評論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡玻驻,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,573評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了偿枕。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片璧瞬。...
    茶點(diǎn)故事閱讀 39,745評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖益老,靈堂內(nèi)的尸體忽然破棺而出彪蓬,到底是詐尸還是另有隱情寸莫,我是刑警寧澤捺萌,帶...
    沈念sama閱讀 35,448評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站膘茎,受9級特大地震影響桃纯,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜披坏,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,048評論 3 327
  • 文/蒙蒙 一态坦、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧棒拂,春花似錦伞梯、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,683評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至攻旦,卻和暖如春喻旷,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背牢屋。 一陣腳步聲響...
    開封第一講書人閱讀 32,838評論 1 269
  • 我被黑心中介騙來泰國打工且预, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留槽袄,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,776評論 2 369
  • 正文 我出身青樓锋谐,卻偏偏與公主長得像遍尺,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子怀估,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,652評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 聲明:算法和數(shù)據(jù)結(jié)構(gòu)的文章均是作者從github上翻譯過來狮鸭,為方便大家閱讀。如果英語閱讀能力強(qiáng)的朋友多搀,可以直接到s...
    UnsanYL閱讀 2,052評論 0 1
  • error code(錯(cuò)誤代碼)=0是操作成功完成歧蕉。error code(錯(cuò)誤代碼)=1是功能錯(cuò)誤。error c...
    Heikki_閱讀 3,379評論 1 9
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,083評論 25 707
  • 我的朋友小黃第一次去西安的時(shí)候想吃點(diǎn)當(dāng)?shù)氐奶禺a(chǎn)康铭,那天我正好有事情惯退,她自己在街上轉(zhuǎn)。 下午回了來后从藤,她說:“我發(fā)現(xiàn)了...
    陳總閱讀 170評論 0 1
  • 《先驅(qū)還是窠臼》 初冬不見凜色 手捧一束野火 扔進(jìn)枯河 顯出惡魔 念幾聲安魂 只余影附和 惡魔不在遍地 惡魔在我心里
    靛絳閱讀 140評論 0 0