1. 程式人生 > 其它 >解壓縮檔案,刪除資料夾下的檔案

解壓縮檔案,刪除資料夾下的檔案

    public void deleteFloder(File floder) {
        File files[] = floder.listFiles();
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
//                System.out.println(files[i].getName());
                if (files[i].isDirectory()) {
                    deleteFloder(files[i]);
                } 
else { files[i].delete(); } } } }
    /**
     * 解壓縮
     * @param inputFile
     * @throws Exception
     */
    public void zipUncompress(String inputFile) throws Exception {
        File srcFile = new File(inputFile);
        // 判斷原始檔是否存在
if (!srcFile.exists()) { throw new Exception(srcFile.getPath() + "所指檔案不存在"); } String destDirPath = inputFile.replace(".zip", ""); // "src/main/kettle/"; //建立壓縮檔案物件 ZipFile zipFile = new ZipFile(srcFile, Charset.forName("GBK")); //開始解壓 Enumeration<?> entries = zipFile.entries();
while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); // 如果是資料夾,就建立個資料夾 if (entry.isDirectory()) { srcFile.mkdirs(); } else { // 如果是檔案,就先建立一個檔案,然後用io流把內容copy過去 File targetFile = new File(destDirPath + "/" + entry.getName()); // 保證這個檔案的父資料夾必須要存在 if (!targetFile.getParentFile().exists()) { targetFile.getParentFile().mkdirs(); } targetFile.createNewFile(); // 將壓縮檔案內容寫入到這個檔案中 InputStream is = zipFile.getInputStream(entry); FileOutputStream fos = new FileOutputStream(targetFile); int len; byte[] buf = new byte[1024]; while ((len = is.read(buf)) != -1) { fos.write(buf, 0, len); } // 關流順序,先開啟的後關閉 fos.close(); is.close(); } } zipFile.close(); }
越努力越幸運~ 加油ヾ(◍°∇°◍)ノ゙