java壓縮檔案

2021-09-07 15:54:44 字數 1822 閱讀 3081

/**

* 壓縮(zip4j)

* @param srcdir 要壓縮資料夾

* @param targetdir zip檔案存放位置

* @param zipname 壓縮檔案名字

* @param encrypt 是否加密

* @param password 密碼

* @return

*/public static boolean tozip(string srcdir, string targetdir, string zipname, boolean encrypt, string password)

arraylistfiles = new arraylist<>(arrays.aslist(srcfiles));

zipparameters parameters = new zipparameters();

parameters.setcompressionmethod(zip4jconstants.comp_deflate);//壓縮方法

parameters.setcompressionlevel(zip4jconstants.deflate_level_ultra);//壓縮級別

parameters.setencryptfiles(encrypt);//是否加密

parameters.setencryptionmethod(zip4jconstants.enc_method_aes);//加密方式

parameters.setaeskeystrength(zip4jconstants.aes_strength_256);//加密強度

parameters.setpassword(password);

zipfile.addfiles(files,parameters);

} catch (zipexception e)

return true;

}/**

*壓縮資料夾到指定目錄 (jdk)

* @param srcdir 壓縮資料夾

* @param targetdir 壓縮檔案存放目錄

* @param zipname 壓縮檔案名字

* @return

*/public static boolean tozip(string srcdir,string targetdir,string zipname)

file srcfiles = src.listfiles();

if( srcfiles == null || srcfiles.length < 1 )

file zipfile = new file(targetdir + zipname );

if( !zipfile.exists() ) catch (ioexception e)

}fileoutputstream fos = null;

zipoutputstream zos = null;

fileinputstream fis = null;

bufferedinputstream bis = null;

try

}}catch (exception e)finally

if(fos != null)

if(fis != null)

if(bis != null)

} catch (ioexception e)

}return true;

}

上面兩種方法各有利弊,zip4j操作簡單,壓縮速度快,可定製化高,但是在系統效能較差的情況下會出現壓縮失敗的現象,而jdk實現的則在系統效能較差時也可以壓縮成功,但是若壓縮比較高的話,壓縮時間也比較長。 

java 壓縮檔案

public static final string filetype zip 壓縮檔案型別 public static string compressedfile string resourcepath,string targetpath,string filename 目的檔案壓縮名 uuid ...

java 壓縮資料夾 zip

purpose 壓縮資料夾 author hermanwang param srcdir 需要壓縮的檔案的路徑 param out zip輸出流 param keepdirstructure 是否保留原來的目錄結構,true 保留目錄結構 false 所有檔案跑到壓縮包根目錄下 注意 不保留目錄結構...

怎樣壓縮檔案 如何給壓縮檔案加密

我們生活 工作中,會經常遇到壓縮檔案 給檔案加密的事情。有時候,是單純不想讓別人看到 有時候,是提供 但不能共享,用來獲取收益等等。那麼怎麼將乙個檔案壓縮,或者乙個資料夾壓縮尼?同時又怎麼給這個壓縮檔案加密尼?下面大家和小編一起看一下吧。壓縮檔案 如圖。找到需要壓縮的檔案 資料夾,右擊檔案,點選新增...