java 壓縮資料夾 zip

2021-08-23 14:13:30 字數 1293 閱讀 9142

/**

* * purpose:壓縮資料夾

* @author hermanwang

* @param srcdir: 需要壓縮的檔案的路徑

* @param out:zip輸出流

* @param keepdirstructure:是否保留原來的目錄結構,true:保留目錄結構;

*                         false:所有檔案跑到壓縮包根目錄下(注意:不保留目錄結構可能會出現同名檔案,會壓縮失敗)

* @throws runtimeexception

* @return void

*/public static void tozip(string srcdir, outputstream out, boolean keepdirstructure) throws runtimeexception catch (exception e) finally catch (ioexception e) }}

}/**

* purpose:遞迴壓縮檔案

* @author hermanwang

* @param sourcefile:原始檔

* @param zos:zip輸出流

* @param name:壓縮後的名稱

* @param keepdirstructure:是否保留原來的目錄結構,true:保留目錄結構;

*                         false:所有檔案跑到壓縮包根目錄下(注意:不保留目錄結構可能會出現同名檔案,會壓縮失敗)

* @throws exception

* @return void

*/private static void compress(file sourcefile, zipoutputstream zos, string name,

boolean keepdirstructure) throws exception

// complete the entry

zos.closeentry();

in.close();

} else

}else else }}

}}呼叫://壓縮成zip

fileoutputstream fos = new fileoutputstream(filepath + file.separator + "要壓縮的檔名" + ".zip");

tozip(filepath + file.separator + "要壓縮的檔名", fos, true);

python壓縮資料夾

usr bin python encoding utf 8 import time import os zip a whole directory and its sub directories and files os.walk 可以得到乙個三元tupple dirpath,dirnames,fi...

VclZip壓縮資料夾

壓縮指定路徑myzipdir下的資料夾b及b目錄下的所有檔案和檔案b.txt function zipdir zipmode integer zipcontrol tvclzip myzipname,myzipdir string boolean begin result false trywith...

python 壓縮資料夾

def zip ya start dir start dir start dir 要壓縮的資料夾路徑 file news start dir zip 壓縮後資料夾的名字 z zipfile.zipfile file news,w zipfile.zip deflated for dir path,d...