1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Bhugy
  • 711
  • 2
  • 8
  • 23

2 Answers2

2

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();
Carlitos Way
  • 3,279
  • 20
  • 30
  • Thanks for your answer... what is h.getPdfRecords()?? Is this method should return paths to all the pdf documents?? or just the names? please help me im really stuck here – Bhugy May 24 '16 at 10:30
  • Yes, `h` is the JAVA Helper that I asume you will use to retrieve the pdf Info from the database.... Ideally, H should return all the info that you need for you problem (for I can see, you only need the pdf filename and pdf file path)... – Carlitos Way May 24 '16 at 13:38
  • Do i need any libraries to do this? because when i use this some keywords(Record for example) doesn't get recognized – Bhugy May 25 '16 at 02:57
  • I have included the io jar n the application compiles fine. But when it comes to IOUtils.copy statement, it suddenly jump ti finally block. – Bhugy May 25 '16 at 05:23
  • Beware! My code is just an example or guide! (copy/paste will not work out of the box) ... Record Class is an hypothetical transfer Object defined only for my example... you should define your own TOs and helper classes (as H) according to your needs!! – Carlitos Way May 25 '16 at 19:33
  • Can you paste your code in your question as an update? What exception are you getting while executing `IOUtils.copy` ?? – Carlitos Way May 25 '16 at 19:35
  • Hey @Carlitos Thank you very much the issue was with my upload function the files werte not the desired location..once i fix it this helps me to continue... I slightly change the code and created the zip file... Thanks a lot.. – Bhugy May 26 '16 at 02:54
1

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

    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
rohit kumar
  • 81
  • 12