使用一组pdf文件创建一个Zip文件 [英] Create one Zip file using a set of pdf files

查看:122
本文介绍了使用一组pdf文件创建一个Zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是一个招标文件系统,其中每个招标编号都附加一个或多个pdf文件.

My app is a tender document system where each tender number has one or more pdf files attached.

应用程序是使用struts和mysql在java ee中完成的.

application is done in java ee using struts and mysql.

在数据库表中存储了招标编号的每个相关pdf文件的路径.

in a database table the paths of each related pdf file for a tender number is stores.

我想获取所有pdf文件,并为每个招标编号创建一个ZIP文件,以便用户可以一次单击下载该zip文件并获取所有相关文档.

I want to get all the pdf files and create a single ZIP file for each tender number so that user can download that zip file and have all the related documents in a single click.

我尝试了Google,发现了一个名为ZipOutputStream的东西,但我不明白如何在我的应用程序中使用它.

I tried Google and found something called ZipOutputStream but i cannot understand how to use this in my application.

推荐答案

您快到了...这是一个如何使用ZipOutputStream的小例子...假设我们有一个Java助手H,返回带有pdf文件路径(和相关信息)的数据库记录:

You're almost there... This is a small example of how to use ZipOutputStream... let's asume that you have a JAVA helper H that returns database records with pdf file paths (and related info):

FileOutputStream zipFile = new FileOutputStream(new File("xxx.zip"));
ZipOutputStream output   = new ZipOutputStream(zipFile);

for (Record r : h.getPdfRecords()) {
    ZipEntry zipEntry = new ZipEntry(r.getPdfName());
    output.putNextEntry(zipEntry);

    FileInputStream pdfFile = new FileInputStream(new File(r.getPath()));
    IOUtils.copy(pdfFile, output);  // this method belongs to apache IO Commons lib!
    pdfFile.close();
    output.closeEntry();
}
output.finish();
output.close();

这篇关于使用一组pdf文件创建一个Zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆