標籤:
一、背景
前端時間,自己做的項目需要打包功能,不想再引外部的jar包
便用java.util.zip做了下該功能
二、適用情境
產生多個word、excel、xml等檔案,並要求打包下載的情形
例:項目資訊的多選匯出word
三、實現
實現分為三個部分,分別是
1、將字串儲存為檔案,都是最基本的IO操作
/** * @Description 將字串儲存為檔案 * @author liuy-8 * @date 2015年5月20日 下午1:48:18 * @param filePath * @param content */ public static void saveFile(String filePath, String content){ File file = new File(filePath); FileWriter fw = null; //這裡沒有使用BufferWrite,是因為資料是一次性寫入 //BufferWrite的優勢在於調用write方法時,使用緩衝區 //而FileWriter每次調用write方法,都調用StreamEncoder的write方法 try { fw = new FileWriter(file);//緩衝區1024字元 fw.write(content); fw.flush(); } catch (IOException e) { e.printStackTrace(); } finally{ //關閉FileWriter try { if(null != fw) fw.close(); } catch (IOException e) { e.printStackTrace(); } } }
2、掃描指定檔案夾下所有的檔案,準備壓縮包的寫入工作
這一部分依然是基本的IO操作,要注意安全關閉輸入輸出資料流,開啟與關閉的順序
/** * @Description 擷取檔案夾中所有檔案路徑 * @author liuy-8 * @date 2015年5月27日 下午5:25:24 * @param folderPath 檔案夾路徑 * @return 檔案夾中所有檔案路徑 */ public static List<String> getFilesPath(String folderPath){ List<String> filesPath = new ArrayList<String>(); File folder = new File(folderPath); File[] fileList = folder.listFiles(); for(File file : fileList){ if(file.isDirectory()){ //是檔案夾,遞迴遍曆 filesPath.addAll(getFilesPath(file.getPath())); }else{ //是檔案,加入檔案清單 filesPath.add(file.getPath()); } } return filesPath; } /** * @Description 壓縮檔夾 * @author liuy-8 * @date 2015年5月26日 下午1:42:00 * @param folderPath 檔案夾路徑 * @param zipFilePath 壓縮檔路徑 */ public static void folder2Zip(String folderPath, String zipFilePath){ //擷取當前系統檔案分隔字元 String fileSeparator = System.getProperty("file.separator"); //若傳入路徑最後沒有檔案分隔字元,加上 if(folderPath.lastIndexOf(fileSeparator) != (folderPath.length() - 1)){ folderPath = folderPath + fileSeparator; } //擷取檔案夾下所有檔案路徑 List<String> filesPath = getFilesPath(folderPath); //檔案輸出資料流 FileOutputStream fos = null; //緩衝輸出資料流 BufferedOutputStream bos = null; //zip輸出資料流 ZipOutputStream zos = null; try { fos = new FileOutputStream(zipFilePath); bos = new BufferedOutputStream(fos); zos = new ZipOutputStream(bos); for(String filePath : filesPath){ //將檔案寫入zip輸出資料流 writeFile2Zip(folderPath, filePath, zos); } zos.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ //關閉zip輸出資料流 try { if(null != zos){ zos.close(); } } catch (IOException e) { e.printStackTrace(); } //關閉緩衝輸出資料流 try { if(null != bos){ bos.close(); } } catch (IOException e) { e.printStackTrace(); } //關閉檔案輸出資料流 try { if(null != fos){ fos.close(); } } catch (IOException e) { e.printStackTrace(); } } }
3、將檔案依次寫入zip輸出資料流
在第二步已經用ZipOutputStream建立了壓縮檔的輸出資料流,這一步裡,每個檔案都要對應一個ZipEntry對象
要將這個對象加入到壓縮檔的輸出資料流中後再寫入資料,並且寫完資料必須在輸出資料流中關閉該ZipEntry對象
這裡有2個容易出錯的地方:
一是ZipEntry對象需要一個路徑,這個路徑是相對於壓縮檔夾的相對路徑,用來保證子檔案夾檔案的順利壓入
二是往zip輸出資料流中寫入的位元組的時候,要判斷迴圈讀取位元組時檔案輸入資料流中讀取了多少位元組長度,讀多少寫多少,不然在檔案尾部會出現錯誤資料
/** * @Description 將檔案寫入zip輸出資料流 * @author liuy-8 * @date 2015年5月27日 下午5:23:42 * @param folderPath 待壓縮檔夾路徑 * @param filePath 待寫入檔案 * @param zos zip輸出資料流 */ private static void writeFile2Zip(String folderPath, String filePath, ZipOutputStream zos){ //緩衝區 byte[] bs = new byte[1024]; //擷取檔案的相對路徑 String entryName = filePath.substring(filePath.indexOf(folderPath) + folderPath.length()); //建立zip實體 ZipEntry entry = new ZipEntry(entryName); //輸入資料流 FileInputStream fis = null; //緩衝輸入資料流 BufferedInputStream bis = null; try { //將zip實體加入zip輸出資料流 zos.putNextEntry(entry); fis = new FileInputStream(filePath); bis = new BufferedInputStream(fis); //寫入zip輸出資料流 int len = 0; while((len = bis.read(bs, 0, 1024)) > 0){ zos.write(bs, 0, len); } zos.flush(); } catch (IOException e) { e.printStackTrace(); } finally{ //關閉entity try { zos.closeEntry(); } catch (IOException e) { e.printStackTrace(); } //關閉BufferedInputStream try { if(null != bis){ bis.close(); } } catch (IOException e) { e.printStackTrace(); } //關閉FileInputStream try { if(null != fis){ bis.close(); } } catch (IOException e) { e.printStackTrace(); } } }
此類已封裝為GlodonFileUtil,有需要找我要即可
java原生檔案打包