import部分不詳述,現在的環境缺甚麼library都會提醒你,以下是確定可執行的程式碼

public class test{
    private static final int BUFFER_SIZE = 4096;

    public static void main(String[] args){
    //解壓縮的檔案 和 目的地資料夾 
        String zipfilepath = "D:\\testfile\\testfile.zip";
        String destdirectory = "D:\\testfile\\unziptest\\";
        try {
               unzip(zipfilepath, destdirectory);
        } catch (IOException e) {
               e.printStackTrace();
        }
    }

    //解壓縮,要將以下部份另外開一個class撰寫亦可
    public static void unzip(String zipfilepath, String destdirectory) throws IOException {
    //判斷目標資料夾路徑是否存在,如果不存在就會自己創一個
        File destDir = new File(destdirectory);
        if (!destDir.exists()) {
            destDir.mkdir();
        }
        ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
        ZipEntry entry = zipIn.getNextEntry();

        while (entry != null) {
            String filePath = destdirectory + File.separator + entry.getName();
            if (!entry.isDirectory()) {
                extractFile(zipIn, filePath);
            } else {
                File dir = new File(filePath);
                dir.mkdir();
            }
            zipIn.closeEntry();
            entry = zipIn.getNextEntry();
        }
        zipIn.close();
    }

    private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
        BufferedOutputStream BOS = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] bytesIn = new byte[BUFFER_SIZE];
        int read = 0;
        while ((read = zipIn.read(bytesIn)) != -1) {
            BOS.write(bytesIn, 0, read);
        }
        BOS.close();
    }
}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 d32318 的頭像
    d32318

    d32318

    d32318 發表在 痞客邦 留言(0) 人氣()