AdSense

網頁

2020/4/11

Java 寫出zip壓縮檔 write out zip file

Java 寫出zip檔的方式如下。

使用ZipOutputStream搭配ZipEntry來輸出zip檔。

package com.abc.demo;

import org.apache.commons.io.FileUtils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {

    public static void main(String[] args) {

        List stringList = generateStringList(); // 產生字串
        String dir = "D:" + File.separator + "tmp"; // zip的輸出檔案目錄
        createDir(dir); // 建立zip輸出目錄資料夾
        String zipName = "hello.zip"; // zip檔名稱
        String pathName = dir + File.separator + zipName; // D:\tmp\hello.zip

        File file = new File(pathName);
        writeZip(file, stringList);

    }

    private static void createDir(String dir) {
        try {
            FileUtils.forceMkdir(new File(dir)); // 建立目錄
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /** 寫出zip檔。每10行寫在一個txt */
    private static void writeZip(File file, List stringList) {
        try (   // Java 7 try-with-resources Statement
                FileOutputStream fos = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(fos);
                ZipOutputStream zos = new ZipOutputStream(bos);
        ) {
            StringBuilder sb = new StringBuilder();
            int lastLineIndex = 0; // 記錄最後一行10倍行數的index
            int txtCount = 0; // 寫出的txt檔計數
            for (int i = 0; i < stringList.size(); i++) {
                String line = stringList.get(i) + System.lineSeparator();
                sb.append(line);
                if (isMultipleOf(10, i + 1)) { // 每10行寫到txt檔
                    ZipEntry entry = new ZipEntry("file" + txtCount++ + ".txt"); // 建立zip檔中的txt檔
                    zos.putNextEntry(entry);
                    zos.write(sb.toString().getBytes()); // 將10行文字寫到txt檔
                    zos.closeEntry();
                    lastLineIndex = i;
                    sb = new StringBuilder();
                }
            }

            // 將剩餘的行寫到最後的txt檔
            for (int i = lastLineIndex + 1; i < stringList.size(); i++) {
                String line = stringList.get(i) + System.lineSeparator();
                sb.append(line);
            }
            ZipEntry entry = new ZipEntry("file" + txtCount + ".txt");
            zos.putNextEntry(entry);
            zos.write(sb.toString().getBytes());
            zos.closeEntry();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /** i是否為m的倍數 */
    private static boolean isMultipleOf(int m, int i) {
        return i % m == 0;
    }

    /** 產生文字內容 */
    public static List generateStringList() {
        List stringList = new ArrayList<>();
        for(int i = 0; i < 103; i++) {
            stringList.add("Hello " + i);
        }
        return stringList;
    }

}

沒有留言:

AdSense