java 通过7z 进行压缩加密

来源:互联网 发布:将乐教育网络办公系统 编辑:程序博客网 时间:2024/05/17 03:39

首先我们先来看一下效果图
这里写图片描述

这里写图片描述

这里写图片描述

要完成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);来运行。
接着只需将运行好的文件以字节的形式输出即可
资源下载

0 0