java压缩文件,加密及不加密
来源:互联网 发布:淘宝外贸原单 编辑:程序博客网 时间:2024/05/18 02:36
首先是对文件的不加密压缩,采用org.apache.tools.zip.ZipEntry,Apache的压缩包压缩
ant包下载地址:http://pan.baidu.com/s/1gdgpioj
代码如下:
package com.zip;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import org.apache.tools.zip.ZipEntry;import org.apache.tools.zip.ZipOutputStream;public class zipDemo {public static void main(String[] args) throws Exception { /* * 测试单个文件的ZIP压缩 * 指定文件路径可以是绝对路径,也可以是相对路径 */ String file = "E:/1.txt"; String zipFile1 = "E:/1.zip"; boolean flag1 = zipSingleFile(file, zipFile1); System.out.println("\n**************压缩【单】个文件<author:pantp>******************************\n"); if (flag1) { System.out.println("单个文件ZIP压缩成功,压缩后文件所在路径为:"+zipFile1); } else { System.out.println("单个文件ZIP压缩失败"); } System.out.println("\n*******************************************************************************"); /* * 测试多个文件的ZIP压缩 * 指定文件路径可以是绝对路径,也可以是相对路径 */ /* String files[] = { "src/zip/文件1.txt", "src/zip/file2.txt" }; String zipFile2 = "src/zip/多个文件压缩.zip"; boolean flag2 = zipFiles(files, zipFile2); System.out.println("\n**************压缩【多】个文件<author:pantp>******************************\n"); if (flag2) { System.out.println("多个文件ZIP压缩成功,压缩后文件所在路径为:"+zipFile2); } else { System.out.println("多个文件ZIP压缩失败"); } System.out.println("\n*******************************************************************************"); */ } /** *<p> *@param file 待压缩文件的名称 例如,src/zip/文件1.txt *@param zipFile 压缩后文件的名称 例如,src/zip/单个文件压缩.zip *@return boolean *@throws :IOException *@Function: zipSingleFile *@Description:单个文件的压缩 *@version : v1.0.0 *@author: pantp *@Date:May 24, 2012 *</p> *Modification History: * Date Author Version Description * --------------------------------------------------------------------- * May 24, 2012 pantp v1.0.0 Create */ public static boolean zipSingleFile(String file, String zipFile) throws IOException { boolean bf = true; File f = new File(file); if (!f.exists()) { System.out.println("文件不存在"); bf = false; } else { File ff = new File(zipFile); if (!f.exists()) { ff.createNewFile(); } // 创建文件输入流对象 FileInputStream in = new FileInputStream(file); // 创建文件输出流对象 FileOutputStream out = new FileOutputStream(zipFile); // 创建ZIP数据输出流对象 ZipOutputStream zipOut = new ZipOutputStream(out); // 得到文件名称 String fileName = file.substring(file.lastIndexOf('/') + 1, file.length()); // 创建指向压缩原始文件的入口 ZipEntry entry = new ZipEntry(fileName); zipOut.putNextEntry(entry); // 向压缩文件中输出数据 int number = 0; byte[] buffer = new byte[512]; while ((number = in.read(buffer)) != -1) { zipOut.write(buffer, 0, number); } zipOut.close(); out.close(); in.close(); } return bf; } /** *<p> *@param files 待压缩的文件列表 例如,src/zip/文件1.txt, src/zip/file2.txt *@param zipfile 压缩后的文件名称 例如,src/zip/多个文件压缩.zip *@return boolean *@throws :Exception *@Function: zipFiles *@Description:多个文件的ZIP压缩 *@version : v1.0.0 *@author: pantp *@Date:May 24, 2012 *</p> *Modification History: * Date Author Version Description * --------------------------------------------------------------------- * May 24, 2012 pantp v1.0.0 Create */ public static boolean zipFiles(String[] files, String zipfile) throws Exception { boolean bf = true; // 根据文件路径构造一个文件实例 File ff = new File(zipfile); // 判断目前文件是否存在,如果不存在,则新建一个 if (!ff.exists()) { ff.createNewFile(); } // 根据文件路径构造一个文件输出流 FileOutputStream out = new FileOutputStream(zipfile); // 传入文件输出流对象,创建ZIP数据输出流对象 ZipOutputStream zipOut = new ZipOutputStream(out); // 循环待压缩的文件列表 for (int i = 0; i < files.length; i++) { File f = new File(files[i]); if (!f.exists()) { bf = false; } try { // 创建文件输入流对象 FileInputStream in = new FileInputStream(files[i]); // 得到当前文件的文件名称 String fileName = files[i].substring( files[i].lastIndexOf('/') + 1, files[i].length()); // 创建指向压缩原始文件的入口 ZipEntry entry = new ZipEntry(fileName); zipOut.putNextEntry(entry); // 向压缩文件中输出数据 int nNumber = 0; byte[] buffer = new byte[512]; while ((nNumber = in.read(buffer)) != -1) { zipOut.write(buffer, 0, nNumber); } // 关闭创建的流对象 in.close(); } catch (IOException e) { e.printStackTrace(); bf = false; } } zipOut.close(); out.close(); return bf; } }
测试效果:
**************压缩【单】个文件<author:pantp>******************************
单个文件ZIP压缩成功,压缩后文件所在路径为:E:/1.zip
*******************************************************************************
对文件加密的:
package com.unzip;import java.io.File;import com.unzip.file.FileUtils;import com.unzip.util.extend.ZipOutput;public class TestZip {public static void main(String[] args) {final String EncryptZipFile = "e:\\a_user.zip";final String password = "123456";final String zipDir = "e:\\a123.txt";System.out.println("===== 加密 =====");File file = new File(zipDir);byte[] zipByte = ZipOutput.getEncryptZipByte(file, password);FileUtils.writeByteFile(zipByte, new File(EncryptZipFile));System.out.println("===== Encrypt Success =====");}}
测试效果:
===== 加密 =====
===== Encrypt Success =====
输入正确的加密密码才可以打开。
java加密源码下载地址:http://pan.baidu.com/s/1i3mqFPF
jar下载地址:http://pan.baidu.com/s/1i3FwVUP
0 0
- java压缩文件,加密及不加密
- java压缩文件 加密
- java AES加密压缩文件
- java自动压缩文件并加密
- 有关java对压缩文件的加密
- java代码实现加密压缩文件解压
- 用WinRAR加密压缩文件
- Java对称加密,非对称加密,不可逆加密Util
- java压缩文件并对压缩包进行加密
- 有关java对压缩文件的加密(通过密码解密)
- MacOS: 如何打开加密压缩文件
- ubuntu破解加密rar压缩文件
- linux下zip加密压缩文件
- java实现将多个文件打包成zip压缩文件以及对压缩文件的加密
- Phalcon Cookies不加密和加密使用方式及注意事项
- Java/JSP安全及加密
- Java进阶(八)Java加密技术之对称加密 非对称加密 不可逆加密算法
- Java进阶(八)Java加密技术之对称加密 非对称加密 不可逆加密算法
- Eclipse配置jdk的方法
- struts2 表单数据绑定map、list等
- 基于Addon-SDK开发火狐扩展
- android canvas.drawBitmap 方法的区别
- 四、用mysql_num_fields()、mysql_fetch_array()、mysql_field_name函数操作数据库
- java压缩文件,加密及不加密
- 经典sql只substr
- Python学习:if语句
- android环境配置
- 年末计划
- phpstrom使用svn创建分、支合并到发布流程
- 评论一下现有几个开源IM框架(Msn/QQ/Fetion/Gtalk…)
- 法律专家谈游戏侵权:如何避免侵权,如何维权
- 五、显示数据库、表内容、表结构