excel打包成zip文件,并且提供加密功能

好久没有写博客了,今天抽空写一个
需求:要将批量导出的excel文件,提供加密的处理功能.

下面贴出完整的代码.
package com.test;

/**
* 将excel打包生成zip文件,并且打开excel
文件的时候,需要输入密码,才可以打开.
作者 aa00aa00
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;

public class TestMailWithEncryptAttachment {

/**
* 生成zip文件
* @param srcfile
* @param zipFileName
* @param password
* @throws IOException
*/
public static void createZipFile(File srcfile, String zipFileName,
String password) throws IOException {

byte b[] = new byte[1024];
ZipOutputStream zout = null;
InputStream in = null;

try {
zout = new ZipOutputStream(new FileOutputStream(zipFileName));
in = new FileInputStream(srcfile);

String filename = srcfile.getName();// 取得文件名
ZipEntry e = new ZipEntry(filename); // 压缩后不带路径
zout.putNextEntry(e);

int len = 0;
while ((len = in.read(b)) != -1) {
zout.write(b, 0, len);
}

zout.closeEntry();
zout.flush();
zout.close();

in.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException io) {
io.printStackTrace();
} finally {
if (zout != null) {
try {
zout.flush();
zout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//encryptZipFile(textFile, zipFileName, password);
TestMailWithEncryptAttachment.createProtectedZip(zipFileName, srcfile,
password);
}

/**
* 生成加密的xls文件
* @param zipFileName
* @param srcfile
* @param password
* @throws IOException
*/
public static void createProtectedZip(String zipFileName, File srcfile, String password) throws IOException
{
try
{
ZipFile zipFile = new ZipFile(zipFileName);

ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(8);
parameters.setCompressionLevel(5);
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(0);
parameters.setPassword(password);
zipFile.addFile(srcfile, parameters);
}
catch (ZipException e)
{
e.printStackTrace();
}
}


/**
* 为压缩文件加密
*
* @param textFile 文本文件
* @param zipFileName 压缩文件名
* @param password 压缩密码

private static void encryptZipFile(File textFile, String zipFileName,
String password) {
AesZipFileEncrypter enc = null;
try {
enc = new AesZipFileEncrypter(zipFileName);
enc.addFileWithoutPath(textFile, password);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
enc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
* @throws IOException
*/

public static void main(String[] args) throws IOException {
// 压缩, 加密文件
String zipFileName = "D:\\testdata\\userInfo.zip";
String compressPassword = "foo";
File srcfile = new File("D:\\testdata\\userInfo.xls");


createZipFile(srcfile, zipFileName, compressPassword);
}

}

附件中是:打包的程序代码和需要使用的jar包.

这里使用zip4j.jar做zip文件的处理,非常的方便.

贴出相关的网站.

http://outofmemory.cn/code-snippet/741/java-usage-ZipOutputStream-pack-zip-file
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值