Create one Zip file using a set of pdf files

余生颓废 提交于 2020-01-06 13:27:08

问题


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

application is done in java ee using struts and mysql.

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

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.

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


回答1:


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();



回答2:


Checkout this code, here you can easily create a zip file directory:

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);
            }

    }
}


来源:https://stackoverflow.com/questions/37404541/create-one-zip-file-using-a-set-of-pdf-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!