简体   繁体   中英

How to Zip Blob value(PDF) in Java

I have a PDF stored in Database as blob. I have a usecase to retrieve the blob file(PDF) and zip it using java. Could anybody help me out here?

Below is my sample code. Where I get my pdf from database as BLobDomain I want it inside a blobfolder and i have given the representation below 807.pdf and 1285.pdf . Thats how i want my blobdomain values to be

 BlobDomain doc1=(BlobDomain)rowshis[0].getAttribute("Document");

 BlobDomain doc2=(BlobDomain)rowshis[1].getAttribute("Document");

        StringBuilder sb = new StringBuilder();

        sb.append("Test Date");
        System.out.println("check----1");

        File f = new File("/customer/scratch/HDLtest.zip");
        ZipOutputStream out = new ZipOutputStream(newFileOutputStream(f));
        System.out.println("check----2");

        ZipEntry e = new ZipEntry("BlobFiles/807.pdf");
        out.putNextEntry(e);
        ZipEntry e11 = new ZipEntry("BlobFiles/1285.pdf");
        out.putNextEntry(e11);
        System.out.println("check----3");
        ZipEntry e1 = new ZipEntry("Test.dat");
        out.putNextEntry(e1);
        System.out.println("check----4");

        byte[] data = sb.toString().getBytes();
        out.write(data, 0, data.length);
        out.closeEntry();
        out.close();

You need to write the content of a zip entry before adding the next zip entry.

Call sequence should be:

  • putNextEntry() - Begins writing a new ZIP file entry and positions the stream to the start of the entry data. Closes the current entry if still active.
  • write() - Writes an array of bytes to the current ZIP entry data.
  • closeEntry() - Closes the current ZIP entry and positions the stream for writing the next entry.
    This call is optional, since calling putNextEntry() for the next entry automatically closes the current entry if still active.

Repeat above for each entry you want to add to the zip file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM