用Java進行zip檔案壓縮與解壓縮
阿新 • • 發佈:2021-01-02
可能存在的業務情況:
1、使用者上傳了壓縮包,需校驗壓縮包中的檔案是否合格。
2、使用者上傳壓縮包,對壓縮包中的檔案進行批量水印處理
解決思路:
1、讀取原壓縮包檔案,解壓縮至臨時目錄
2、對臨時目錄中的解壓縮檔案進行校驗/水印處理
3、對臨時目錄中處理過的檔案進行壓縮
4、刪除臨時目錄及其下的檔案
需要參考前面的校驗PDF的帖子
測試結果如下:
測試程式碼:
@Test public void testZip() { String filePath = "D:\\pdfTest\\合格壓縮檔案.zip"; // 獲取原文所在目錄 // 伺服器上時,檔案路徑為“/”,此處測試需要換成filePath中的“\\” //String oldFilePath = filePath.substring(0,filePath.lastIndexOf("/")); String oldFilePath = filePath.substring(0,filePath.lastIndexOf("\\")); System.out.println("原檔案路徑:" + oldFilePath); // 臨時目錄,原壓縮檔案解壓目錄 String destDirPath = oldFilePath + "\\tmp\\"; System.out.println("臨時路徑:" + destDirPath); // 將原壓縮檔案解壓到臨時目錄 ZipUtil.unzipFile(filePath,destDirPath); // 臨時目錄檔案物件 File destDir = new File(destDirPath); // 獲取臨時目錄下的所有檔案 File[] files = destDir.listFiles(); // 定義變數,儲存校驗結果 List<Integer> list = new ArrayList<>(); // 遍歷檔案,進行校驗 for (File file: files) { String absolutePath = file.getAbsolutePath(); System.out.println(absolutePath); int i = CheckPdfHelper.checkPdf(absolutePath); list.add(i); // 壓縮包中存在不合格PDF檔案時 if (i != 0) { break; } } // 判斷是否包含不合格PDF檔案 if (list.contains(1)) { System.out.println("壓縮檔案中包含不合格PDF檔案"); // 刪除解壓縮的檔案和臨時目錄 ZipUtil.deletefile(destDirPath); // 不合格時,不生成新的壓縮包檔案 return; } else { System.out.println("壓縮檔案PDF檔案均符合要求"); } // 獲取原壓縮檔案字尾 int pos = filePath.lastIndexOf('.'); String suffix = filePath.substring(pos + 1); // 新生成壓縮檔案路徑 String newFilePath = filePath.substring(0,pos) + ".PSW." + suffix; System.out.println("新的壓縮檔案路徑:" + newFilePath); // 將檢驗成功的檔案壓縮成一個新的壓縮包 ZipUtil.zipFile(newFilePath,files); // 刪除臨時目錄 ZipUtil.deletefile(destDirPath); }
ZipUtil工具類:
package com.alphajuns.util; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; import org.apache.tools.zip.ZipOutputStream; /** * @ClassName ZipUtil * @Description 壓縮或解壓縮zip:由於直接使用java.util.zip工具包下的類,會出現中文亂碼問題,所以使用ant.jar中的org.apache.tools.zip下的工具類 * @Author AlphaJunS * @Date 2020/3/8 11:30 * @Version 1.0 */ public class ZipUtil { /** * @Author AlphaJunS * @Date 11:32 2020/3/8 * @Description * @param zip 壓縮目的地址 * @param srcFiles 壓縮的原始檔 * @return void */ public static void zipFile( String zip,File[] srcFiles ) { try { if( zip.endsWith(".zip") || zip.endsWith(".ZIP") ){ FileOutputStream fos = new FileOutputStream(new File(zip)); ZipOutputStream _zipOut = new ZipOutputStream(fos) ; _zipOut.setEncoding("GBK"); for( File _f : srcFiles ){ handlerFile(zip,_zipOut,_f,""); } fos.close(); _zipOut.close(); }else{ System.out.println("target file[" + zip + "] is not .zip type file"); } } catch (FileNotFoundException e) { } catch (IOException e) { } } /** * @Author AlphaJunS * @Date 11:33 2020/3/8 * @Description * @param zip 壓縮的目的地址 * @param zipOut * @param srcFile 被壓縮的檔案資訊 * @param path 在zip中的相對路徑 * @return void */ private static void handlerFile(String zip,ZipOutputStream zipOut,File srcFile,String path) throws IOException { System.out.println(" begin to compression file[" + srcFile.getName() + "]"); if( !"".equals(path) && ! path.endsWith(File.separator)){ path += File.separator ; } if( ! srcFile.getPath().equals(zip) ){ if( srcFile.isDirectory() ){ File[] _files = srcFile.listFiles() ; if( _files.length == 0 ){ zipOut.putNextEntry(new ZipEntry( path + srcFile.getName() + File.separator)); zipOut.closeEntry(); }else{ for( File _f : _files ){ handlerFile( zip,zipOut,path + srcFile.getName() ); } } }else{ InputStream _in = new FileInputStream(srcFile) ; zipOut.putNextEntry(new ZipEntry(path + srcFile.getName())); int len = 0 ; byte[] _byte = new byte[1024]; while( (len = _in.read(_byte)) > 0 ){ zipOut.write(_byte,len); } _in.close(); zipOut.closeEntry(); } } } /** * @Author AlphaJunS * @Date 11:34 2020/3/8 * @Description 解壓縮ZIP檔案,將ZIP檔案裡的內容解壓到targetDIR目錄下 * @param zipPath 待解壓縮的ZIP檔名 * @param descDir 目標目錄 * @return java.util.List<java.io.File> */ public static List<File> unzipFile(String zipPath,String descDir) { return unzipFile(new File(zipPath),descDir) ; } /** * @Author AlphaJunS * @Date 11:36 2020/3/8 * @Description 對.zip檔案進行解壓縮 * @param zipFile 解壓縮檔案 * @param descDir 壓縮的目標地址,如:D:\\測試 或 /mnt/d/測試 * @return java.util.List<java.io.File> */ @SuppressWarnings("rawtypes") public static List<File> unzipFile(File zipFile,String descDir) { List<File> _list = new ArrayList<File>() ; try { ZipFile _zipFile = new ZipFile(zipFile,"GBK") ; for( Enumeration entries = _zipFile.getEntries() ; entries.hasMoreElements() ; ){ ZipEntry entry = (ZipEntry)entries.nextElement() ; File _file = new File(descDir + File.separator + entry.getName()) ; if( entry.isDirectory() ){ _file.mkdirs() ; }else{ File _parent = _file.getParentFile() ; if( !_parent.exists() ){ _parent.mkdirs() ; } InputStream _in = _zipFile.getInputStream(entry); OutputStream _out = new FileOutputStream(_file) ; int len = 0 ; byte[] _byte = new byte[1024]; while( (len = _in.read(_byte)) > 0){ _out.write(_byte,len); } _in.close(); _out.flush(); _out.close(); _list.add(_file) ; } } } catch (IOException e) { } return _list ; } /** * @Author AlphaJunS * @Date 11:36 2020/3/8 * @Description 對臨時生成的資料夾和資料夾下的檔案進行刪除 * @param delpath * @return void */ public static void deletefile(String delpath) { try { File file = new File(delpath); if (!file.isDirectory()) { file.delete(); } else if (file.isDirectory()) { String[] fileList = file.list(); for (int i = 0; i < fileList.length; i++) { File delfile = new File(delpath + File.separator + fileList[i]); if (!delfile.isDirectory()) { delfile.delete(); } else if (delfile.isDirectory()) { deletefile(delpath + File.separator + fileList[i]); } } file.delete(); } } catch (Exception e) { e.printStackTrace(); } } }
以上就是用Java進行zip檔案壓縮與解壓縮的詳細內容,更多關於java zip檔案壓縮與解壓縮的資料請關注我們其它相關文章!