我的应用程序是一个招标文件系统,每个投标号码附有一个或多个pdf文件。
使用struts和mysql在java ee中完成应用程序。
在数据库表中,招标编号的每个相关pdf文件的路径都是商店。
我想获取所有pdf文件并为每个投标号创建一个ZIP文件,以便用户可以下载该zip文件并只需单击即可获得所有相关文档。
我尝试了Google并找到了一个名为ZipOutputStream
的内容,但我无法理解如何在我的应用程序中使用它。
答案 0 :(得分:1)
你差不多......这是一个如何使用ZipOutputStream
的小例子...让我们假设你有一个JAVA帮助器H,它返回带有pdf文件路径的数据库记录(和相关信息) :
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();
答案 1 :(得分:1)
签出此代码,在这里您可以轻松创建一个zip文件目录:
public class CreateZipFileDirectory {
public static void main(String args[])
{
try
{
String zipFile = "C:/FileIO/zipdemo.zip";
String sourceDirectory = "C:/examples";
//create byte buffer
byte[] buffer = new byte[1024];
FileOutputStream fout = new FileOutputStream(zipFile);
ZipOutputStream zout = new ZipOutputStream(fout);
File dir = new File(sourceDirectory);
if(!dir.isDirectory())
{
System.out.println(sourceDirectory + " is not a directory");
}
else
{
File[] files = dir.listFiles();
for(int i=0; i < files.length ; i++)
{
System.out.println("Adding " + files[i].getName());
FileInputStream fin = new FileInputStream(files[i]);
zout.putNextEntry(new ZipEntry(files[i].getName()));
int length;
while((length = fin.read(buffer)) > 0)
{
zout.write(buffer, 0, length);
}
zout.closeEntry();
fin.close();
}
}
zout.close();
System.out.println("Zip file has been created!");
}
catch(IOException ioe)
{
System.out.println("IOException :" + ioe);
}
}
}