【问题标题】:Create one Zip file using a set of pdf files使用一组 pdf 文件创建一个 Zip 文件
【发布时间】:2016-05-24 04:43:00
【问题描述】:

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

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

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

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

我尝试了谷歌并找到了一个名为 ZipOutputStream 的东西,但我不明白如何在我的应用程序中使用它。

【问题讨论】:

    标签: java mysql pdf zipoutputstream


    【解决方案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();
    

    【讨论】:

    • 感谢您的回答... h.getPdfRecords() 是什么?这种方法是否应该返回所有pdf文档的路径?还是只是名字?请帮助我,我真的被困在这里了
    • 是的,h 是我认为您将用来从数据库中检索 pdf 信息的 JAVA 助手。理想情况下,H 应该返回您需要的所有信息问题(我可以看到,您只需要 pdf 文件名和 pdf 文件路径)...
    • 我需要任何库来执行此操作吗?因为当我使用这个时,一些关键字(例如记录)不会被识别
    • 我已将 io jar 包含在应用程序编译好的中。但是一到IOUtils.copy语句,就突然跳到finally块了。
    • 当心!我的代码只是一个示例或指南! (复制/粘贴不能开箱即用)...记录类是仅为我的示例定义的假设传输对象...您应该根据需要定义自己的 TO 和辅助类(如 H)!!
    【解决方案2】:

    签出这段代码,在这里你可以轻松创建一个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);
                }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-10-16
      • 2013-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-22
      • 1970-01-01
      相关资源
      最近更新 更多