用Java進(jìn)行zip文件壓縮與解壓縮
1、用戶上傳了壓縮包,需校驗(yàn)壓縮包中的文件是否合格。
2、用戶上傳壓縮包,對(duì)壓縮包中的文件進(jìn)行批量水印處理
解決思路:1、讀取原壓縮包文件,解壓縮至臨時(shí)目錄
2、對(duì)臨時(shí)目錄中的解壓縮文件進(jìn)行校驗(yàn)/水印處理
3、對(duì)臨時(shí)目錄中處理過的文件進(jìn)行壓縮
4、刪除臨時(shí)目錄及其下的文件
需要參考前面的校驗(yàn)PDF的帖子
測試結(jié)果如下:
測試代碼:
@Test public void testZip() { String filePath = 'D:pdfTest合格壓縮文件.zip'; // 獲取原文所在目錄 // 服務(wù)器上時(shí),文件路徑為“/”,此處測試需要換成filePath中的“” //String oldFilePath = filePath.substring(0, filePath.lastIndexOf('/')); String oldFilePath = filePath.substring(0, filePath.lastIndexOf('')); System.out.println('原文件路徑:' + oldFilePath); // 臨時(shí)目錄,原壓縮文件解壓目錄 String destDirPath = oldFilePath + 'tmp'; System.out.println('臨時(shí)路徑:' + destDirPath); // 將原壓縮文件解壓到臨時(shí)目錄 ZipUtil.unzipFile(filePath, destDirPath); // 臨時(shí)目錄文件對(duì)象 File destDir = new File(destDirPath); // 獲取臨時(shí)目錄下的所有文件 File[] files = destDir.listFiles(); // 定義變量,保存校驗(yàn)結(jié)果 List<Integer> list = new ArrayList<>(); // 遍歷文件,進(jìn)行校驗(yàn) for (File file: files) { String absolutePath = file.getAbsolutePath(); System.out.println(absolutePath); int i = CheckPdfHelper.checkPdf(absolutePath); list.add(i); // 壓縮包中存在不合格PDF文件時(shí) if (i != 0) {break; } } // 判斷是否包含不合格PDF文件 if (list.contains(1)) { System.out.println('壓縮文件中包含不合格PDF文件'); // 刪除解壓縮的文件和臨時(shí)目錄 ZipUtil.deletefile(destDirPath); // 不合格時(shí),不生成新的壓縮包文件 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); // 將檢驗(yàn)成功的文件壓縮成一個(gè)新的壓縮包 ZipUtil.zipFile(newFilePath, files); // 刪除臨時(shí)目錄 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工具包下的類,會(huì)出現(xiàn)中文亂碼問題,所以使用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中的相對(duì)路徑 * @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 , _f , 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, 0, len);}_in.close();zipOut.closeEntry(); } } } /** * @Author AlphaJunS * @Date 11:34 2020/3/8 * @Description 解壓縮ZIP文件,將ZIP文件里的內(nèi)容解壓到targetDIR目錄下 * @param zipPath 待解壓縮的ZIP文件名 * @param descDir 目標(biāo)目錄 * @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 對(duì).zip文件進(jìn)行解壓縮 * @param zipFile 解壓縮文件 * @param descDir 壓縮的目標(biāo)地址,如: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, 0, len); } _in.close(); _out.flush(); _out.close(); _list.add(_file) ;} } } catch (IOException e) { } return _list ; } /** * @Author AlphaJunS * @Date 11:36 2020/3/8 * @Description 對(duì)臨時(shí)生成的文件夾和文件夾下的文件進(jìn)行刪除 * @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進(jìn)行zip文件壓縮與解壓縮的詳細(xì)內(nèi)容,更多關(guān)于java zip文件壓縮與解壓縮的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. python爬蟲beautifulsoup解析html方法2. Python 如何將integer轉(zhuǎn)化為羅馬數(shù)(3999以內(nèi))3. python 實(shí)現(xiàn)aes256加密4. 詳解Python模塊化編程與裝飾器5. css進(jìn)階學(xué)習(xí) 選擇符6. Python性能測試工具Locust安裝及使用7. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式8. 使用Python解析Chrome瀏覽器書簽的示例9. html小技巧之td,div標(biāo)簽里內(nèi)容不換行10. python web框架的總結(jié)
