首先我們先來看一下效果圖
要完成7z的壓縮加密,我們首先先要下載7z工具,這個可以到官網自行下載,接下來我們只需將7z.dll,7z.exe,7z.sfx將這幾個文件放在我們的工程目錄,然後去引用即可。
關鍵代碼示例:
private boolean zipByExternal(String zipFileName, String[] zipContentFileNameList, String compressionType, String password) throws Exception {
Runtime runtime = null;
Process process = null;
String command = "";
StringBuffer sbFilePath = null;
StringBuffer sbZipResult = null;
String zipLibPath = "";
String result = "";
InputStream inputStream = null;
int value = 0;
boolean isSuccess = false;
try{
if (!isContainData(zipFileName)){
throw new Exception(ERROR_MSG_NOT_OUTPUT_PATH);
}
if (zipContentFileNameList != null && zipContentFileNameList.length > 0){
for (int i=0; i<zipContentFileNameList.length; i++){
if (!FileUtil.fileIsExist(zipContentFileNameList[i])){
throw new Exception(ERROR_MSG_COMPRESSION_NO_ATTACHMENT_FILE);
}
}
}else{
throw new Exception(ERROR_MSG_COMPRESSION_NO_ATTACHMENT_FILE);
}
if (!isContainData(compressionType)){
throw new Exception(ERROR_MSG_NOT_OUTPUT_PATH);
}
if (!isContainData(compressionType)){
throw new Exception(ERROR_MSG_COMPRESSION_INVALID_TYPE);
}
zipLibPath = Sevent_HOME_PATH + schoolId + File.separator + Sevent_COMMON_PATH + Sevent_ZIP_PATH;
runtime = Runtime.getRuntime();
sbFilePath = new StringBuffer(100);
sbZipResult = new StringBuffer();
for (int i=0; i<zipContentFileNameList.length; i++){
sbFilePath.append(zipContentFileNameList[i] + " ");
}
if (COMPRESSION_TYPE_EXE.equals(compressionType)){
if (zipFileName.toLowerCase().indexOf(COMPRESSION_TYPE_EXE.toLowerCase()) < 0){
throw new Exception(ERROR_MSG_COMPRESSION_TYPE_NOT_MATCH);
}
if (isContainData(password)){
//command = zipLibPath + Sevent_ZIP_EXE +" a -p" + password + " -mhe -sfx7z.sfx " + zipFileName + " " + sbFilePath;
command = root_path + File.separator + Sevent_ZIP_EXE +" a -p" + password + " -mhe -sfx7z.sfx " + zipFileName + " " + sbFilePath;
}else{
//command = zipLibPath + Sevent_ZIP_EXE +" a -mhe -sfx7z.sfx " + zipFileName + " " + sbFilePath;
command = root_path + File.separator + Sevent_ZIP_EXE +" a -mhe -sfx7z.sfx " + zipFileName + " " + sbFilePath;
}
}else if (COMPRESSION_TYPE_ZIP.equals(compressionType)){
if (zipFileName.toLowerCase().indexOf(COMPRESSION_TYPE_ZIP.toLowerCase()) < 0){
throw new Exception(ERROR_MSG_COMPRESSION_TYPE_NOT_MATCH);
}
if (isContainData(password)){
//command = zipLibPath + Sevent_ZIP_EXE +" a -mem=aes -p" + password + " -tzip " + zipFileName + " " + sbFilePath;
command = root_path + File.separator + Sevent_ZIP_EXE +" a -mem=aes -p" + password + " -tzip " + zipFileName + " " + sbFilePath;
}else{
//command = zipLibPath + Sevent_ZIP_EXE +" a -mem=aes -tzip " + zipFileName + " " + sbFilePath;
command = root_path + File.separator + Sevent_ZIP_EXE +" a -mem=aes -tzip " + zipFileName + " " + sbFilePath;
}
}
process = runtime.exec(command);
inputStream = process.getInputStream();
while ((value = inputStream.read()) != -1)
{
sbZipResult.append((char) value);
}
result = new String(sbZipResult.toString().getBytes("ISO-8859-1"), "GBK");
if (result.toLowerCase().indexOf(SUCCESS_KEYWORD.toLowerCase()) >= 0){
isSuccess = true;
}
inputStream.close();
inputStream = null;
}catch (Exception e){
e.printStackTrace();
throw new Exception(ERROR_MSG_UNKNOWN_ERROR);
}finally{
try{
if (inputStream != null){
inputStream.close();
inputStream = null;
}
}catch (Exception e){}
return isSuccess;
}
}
由示例代碼可知,我們這裏會創建一個線程來執行我們的壓縮動作,
由於7z支持exe壓縮和zip壓縮,所以這裏我們做了類型判斷,接下來只需要執行7z自帶的命令即可進行打包壓縮了,由於這裏是調用shell命令,所以我們只能通過執行runtime.exec(command);來運行。
接着只需將運行好的文件以字節的形式輸出即可
資源下載