中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Java壓縮和解壓文件工具類ZipUtil

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用
用于服務(wù)器端打包文件的,將壓縮后的文件寫入到response輸出流即可實(shí)現(xiàn)在服務(wù)器端打包下載,支持多級(jí)目錄嵌套。
測(cè)試時(shí)可以先用ZipUtil.zip壓縮某個(gè)文件夾test,得到壓縮文件test.zip,然后將文件夾test刪除,用ZipUtil.unzip解壓test.zip即可還原。

PS:需要解決中文亂碼的朋友可以參考此處 http://log-cd.iteye.com/blog/585647
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
 
 
/**
 * 通過Java的Zip輸入輸出流實(shí)現(xiàn)壓縮和解壓文件
 * 
 * @author liujiduo
 * 
 */
public final class ZipUtil {
 
    private static byte[] buffer = new byte[1024 * 10];
 
    private ZipUtil() {
        // empty
    }
 
    /**
     * 壓縮文件
     * 
     * @param filePath
     *            待壓縮的文件路徑
     * @return 壓縮后的文件
     */
    public static File zip(String filePath) {
        File target = null;
        File source = new File(filePath);
        if (source.exists()) {
            // 壓縮文件名=源文件名.zip
            String zipName = source.getName() + ".zip";
            target = new File(source.getParent(), zipName);
            if (target.exists()) {
                target.delete(); // 刪除舊的文件
            }
            FileOutputStream fos = null;
            ZipOutputStream zos = null;
            try {
                fos = new FileOutputStream(target);
                zos = new ZipOutputStream(new BufferedOutputStream(fos));
                // 添加對(duì)應(yīng)的文件Entry
                addEntry("/", source, zos);
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                IOUtil.closeQuietly(zos, fos);
            }
        }
        return target;
    }
 
    /**
     * 掃描添加文件Entry
     * 
     * @param base
     *            基路徑
     * 
     * @param source
     *            源文件
     * @param zos
     *            Zip文件輸出流
     * @throws IOException
     */
    private static void addEntry(String base, File source, ZipOutputStream zos)
            throws IOException {
        // 按目錄分級(jí),形如:/aaa/bbb.txt
        String entry = base + source.getName();
        if (source.isDirectory()) {
            for (File file : source.listFiles()) {
                // 遞歸列出目錄下的所有文件,添加文件Entry
                addEntry(entry + "/", file, zos);
            }
        } else {
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(source);
                bis = new BufferedInputStream(fis, buffer.length);
                int read = 0;
                zos.putNextEntry(new ZipEntry(entry));
                while ((read = bis.read(buffer, 0, buffer.length)) != -1) {
                    zos.write(buffer, 0, read);
                }
                zos.closeEntry();
            } finally {
                IOUtil.closeQuietly(bis, fis);
            }
        }
    }
 
    /**
     * 解壓文件
     * 
     * @param filePath
     *            壓縮文件路徑
     */
    public static void unzip(String filePath) {
        File source = new File(filePath);
        if (source.exists()) {
            ZipInputStream zis = null;
            BufferedOutputStream bos = null;
            try {
                zis = new ZipInputStream(new FileInputStream(source));
                ZipEntry entry = null;
                while ((entry = zis.getNextEntry()) != null && !entry.isDirectory()) {
                    File target = new File(source.getParent(), entry.getName());
                    if (!target.getParentFile().exists()) {
                        // 創(chuàng)建文件父目錄
                        target.getParentFile().mkdirs();
                    }
                    // 寫入文件
                    bos = new BufferedOutputStream(new FileOutputStream(target));
                    int read = 0;
                    while ((read = zis.read(buffer, 0, buffer.length)) != -1) {
                        bos.write(buffer, 0, read);
                    }
                    bos.flush();
                }
                zis.closeEntry();
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                IOUtil.closeQuietly(zis, bos);
            }
        }
    }
 
    public static void main(String[] args) {
        String targetPath = "E:\\Win7壁紙";
        File file = ZipUtil.zip(targetPath);
        System.out.println(file);
        ZipUtil.unzip("F:\\Win7壁紙.zip");
    }
}
import java.io.Closeable;
import java.io.IOException;
 
/**
 * IO流工具類
 * 
 * @author liujiduo
 * 
 */
public class IOUtil {
    /**
     * 關(guān)閉一個(gè)或多個(gè)流對(duì)象
     * 
     * @param closeables
     *            可關(guān)閉的流對(duì)象列表
     * @throws IOException
     */
    public static void close(Closeable... closeables) throws IOException {
        if (closeables != null) {
            for (Closeable closeable : closeables) {
                if (closeable != null) {
                    closeable.close();
                }
            }
        }
    }
 
    /**
     * 關(guān)閉一個(gè)或多個(gè)流對(duì)象
     * 
     * @param closeables
     *            可關(guān)閉的流對(duì)象列表
     */
    public static void closeQuietly(Closeable... closeables) {
        try {
            close(closeables);
        } catch (IOException e) {
            // do nothing
        }
    }
 
}


標(biāo)簽: 服務(wù)器 服務(wù)器端

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:Android利用后臺(tái)服務(wù)下載網(wǎng)絡(luò)數(shù)據(jù)

下一篇:C# 托盤程序 實(shí)例 雙擊顯示窗體,最小化到托盤