xiaozhi7616 2011-02-10 15:15
浏览 493
已采纳

java生成zip文件,使用密码加密的问题

我在项目中有这样的需求:使用java生成zip文件,然后使用密码加密生成好的zip文件,分别将zip文件和密码发送两封邮件给用户,用户使用密码解压zip文件。

现在zip文件生成没有问题,但是使用密码加密始终没有搞定,请教各位有没有什么办法可以解决?

我在网上找到一篇文章讲类似的解决方案
[url=http://java.sys-con.com/node/1258827]http://java.sys-con.com/node/1258827[/url]

文中使用了“winzipaes”,“sevenzipjbind”,“Bouncecastle",但是实验没有成功,中间的问题在于他使用了几个外部的类库分别是:

[list]
[*]passwordcompressor.jar
[*]sevenzipjbinding-AllPlatforms.jar
[*]sevenzipjbinding.jar
[*]bcprov-jdk15-145.jar
[/list]

“winzipaes”中没有文中中提到的passwordcompressor.jar,只有源文件,打成jar文件之后,能生成加密zip文件,但是解压缩失败。

这个问题解决了,代码是没有问题的,测试解压的软件使用的是WinRAR.36X的版本,不能正常解压缩,使用WinRAR3.80及以上版本是没有问题的。

下面出现了新的问题,只用压缩文件(未加密)作为附件发送邮件很正常没有问题。但是如果问价加密后,就不能正常发送邮件了,代码和log都是正常的,但就是接受不到邮件,比较郁闷。

[code="java"]

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.sql.Timestamp;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.globex.pp.common.util.DateUtil;
import com.globex.pp.web.util.MailUtil;

import de.idyl.crypto.zip.AesZipFileEncrypter;
import de.schlichtherle.util.zip.ZipEntry;
import de.schlichtherle.util.zip.ZipOutputStream;

public class TestMailWithEncryptAttachment {

protected Log logger = LogFactory.getLog(getClass());

private static Map<String, String> sendMails(String userName, String zipFile,
        String compressPassword) {
    Map<String, String> map = new HashMap<String, String>();

    String smtpServer = "smtp.163.com";
    String smtpUsername = "XXX";
    String smtpPassword = "XXX";
    String from = "XXX@163.com";
    String displayName = "Mail Sender";

    String currentDate = DateUtil.formatDateCN(new Timestamp(System
            .currentTimeMillis()));
    String currentTime = DateUtil.formatDateTimeCN(new Timestamp(System
            .currentTimeMillis()));

    String productName = "《XXXX》";
    String surffix = "信息领取邮件";
    String type_attachment = "_附件";
    String type_password = "_密码";

    // String to = user.getEmail();
    // FIXME: delete me
    String to = "XXX@163.com";

    map = sendMail4Attachment(smtpServer, smtpUsername, smtpPassword, from,
            displayName, userName, zipFile, currentDate, currentTime,
            productName, surffix, type_attachment, to);

    printMailMessages(map);
    map.clear();

    map = sendMail4Password(smtpServer, smtpUsername, smtpPassword, from,
            displayName, userName, compressPassword, currentDate,
            currentTime, productName, surffix, type_password, to);

    printMailMessages(map);

    return map;
}

/**
 * 发送加密的激活码信息
 * 
 * @param smtpServer
 *            SMTP服务器地址
 * @param smtpUsername
 *            SMTP用户名
 * @param smtpPassword
 *            SMTP密码
 * @param from
 *            发件人
 * @param displayName
 *            发件人显示名称
 * @param userName
 *            当前用户
 * @param zipFile
 * @param currentDate
 * @param currentTime
 * @param productName
 * @param surffix
 * @param type_attachment
 * @param to
 *            收件人地址
 * @return
 */
private static Map<String, String> sendMail4Attachment(String smtpServer,
        String smtpUsername, String smtpPassword, String from,
        String displayName, String userName, String zipFile,
        String currentDate, String currentTime, String productName,
        String surffix, String type_attachment, String to) {
    Map<String, String> map = new HashMap<String, String>();

    String subject = currentDate + productName + surffix + type_attachment;
    String content = userName + " 您好: 您在 " + currentTime
            + " 领取的激活码在邮件附件(请使用WinRAR3.80以上版本解压缩该附件)中,请查收。"
            + "解压密码在下一封邮件中。谢谢。";

    MailUtil m = new MailUtil(smtpServer, from, displayName, smtpUsername,
            smtpPassword, to, subject, content);
    m.addAttachfile(zipFile);
    map = m.send();
    return map;
}

/**
 * 发送加密文件的解压密码
 * 
 * @param smtpServer
 *            SMTP服务器地址
 * @param smtpUsername
 *            SMTP用户名
 * @param smtpPassword
 *            SMTP密码
 * @param from
 *            发件人
 * @param displayName
 *            发件人显示名称
 * @param userName
 *            当前用户
 * @param compressPassword
 * @param currentDate
 * @param currentTime
 * @param productName
 * @param surffix
 * @param type_password
 * @param to
 *            收件人地址
 * @return
 */
private static Map<String, String> sendMail4Password(String smtpServer,
        String smtpUsername, String smtpPassword, String from,
        String displayName, String userName, String compressPassword,
        String currentDate, String currentTime, String productName,
        String surffix, String type_password, String to) {
    Map<String, String> map = new HashMap<String, String>();

    String subject = currentDate + productName + surffix + type_password;
    String content = userName + " 您好: " + "您在 " + currentTime
            + " 领取的激活码压缩包密码为" + compressPassword + "。谢谢。";

    MailUtil m = new MailUtil(smtpServer, from, displayName, smtpUsername,
            smtpPassword, to, subject, content);
    map = m.send();

    return map;
}

/**
 * 打印邮件发送信息
 * 
 * @param map
 */
private static void printMailMessages(Map<String, String> map) {
    Set<Map.Entry<String, String>> set = map.entrySet();
    Iterator<Map.Entry<String, String>> it = set.iterator();
    while (it.hasNext()) {
        Map.Entry<String, String> entry = it.next();
        System.out.println("Email Log: " + entry.getKey() + " - "
                + entry.getValue());
    }
}

public static void createZipFile(File textFile, String zipFileName,
        String password) {

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

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

        String filename = textFile.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);
}

/**
 * 为压缩文件加密
 * 
 * @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();
        }
    }
}

public static void main(String[] args) {
    // 压缩, 加密文件
    String zipFileName = "D:\\testdata\\ActiveCode.rar";
    String compressPassword = "foo";
    File textFile = new File("D:\\testdata\\ActiveCode.txt");

    createZipFile(textFile, zipFileName, compressPassword);

    // 发邮件, 返回邮件发送信息
    sendMails("Mail Receiver", zipFileName, compressPassword);
}

}

[/code]

  • 写回答

1条回答

  • CaiHuajiang 2011-02-11 16:52
    关注

    不知道那所用的组件是什么版本,我没有找到AesZipFileEncrypter的addFileWithoutPath方法。

    总体来说,直接看没什么问题。
    你还是检查一下加密的文件是否生成,并且能否打开吧。如果并未生成,或者生成错误的文件。那你再将不加密与加密的两段逻辑分开试试

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何提取csv文件中需要的列,将其整合为一篇完整文档,并进行jieba分词(语言-python)
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?