简体   繁体   English

如何用Java压缩Blob值(PDF)

[英]How to Zip Blob value(PDF) in Java

I have a PDF stored in Database as blob. 我有一个PDF以blob格式存储在数据库中。 I have a usecase to retrieve the blob file(PDF) and zip it using java. 我有一个用例来检索blob文件(PDF)并使用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 . 我从数据库中以BLobDomain的形式获取pdf,我希望将其保存在blobfolder中,并给出了下面的表示形式807.pdf和1285.pdf。 Thats how i want my blobdomain values to be 那就是我希望我的blobdomain值是

 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. 在添加下一个zip条目之前,您需要编写zip条目的内容。

Call sequence should be: 呼叫顺序应为:

  • putNextEntry() - Begins writing a new ZIP file entry and positions the stream to the start of the entry data. putNextEntry() - 开始写入新的ZIP文件条目,并将流定位到条目数据的开头。 Closes the current entry if still active. 如果仍处于活动状态,则关闭当前条目。
  • write() - Writes an array of bytes to the current ZIP entry data. write() - 将字节数组写入当前的ZIP条目数据。
  • closeEntry() - Closes the current ZIP entry and positions the stream for writing the next entry. closeEntry() - 关闭当前的ZIP条目并放置流以写入下一个条目。
    This call is optional, since calling putNextEntry() for the next entry automatically closes the current entry if still active. 该调用是可选的,因为调用下一个条目的putNextEntry()自动关闭当前条目(如果仍处于活动状态)。

Repeat above for each entry you want to add to the zip file. 对要添加到zip文件中的每个条目重复上述步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将Blob object转换成Java中的PDF文件? - How to convert a Blob object into a PDF file in Java? 如何将生成的 pdf 保存为 Java 中的 blob - How to save generated pdf as blob in Java 如何将zip文件移动到Java中的blob列? - How do I move zip file to blob column in Java? 如何使用 java 从 oracle blob 中检索 zip 文件? - How to retrieve an zip file from oracle blob using java? 如何使用Java从MySql数据库中检索Blob pdf文件 - How to retrive Blob pdf file from MySql database using java 如何使用Java从Blob插入和检索pdf - how to insert and retrieve pdf from blob using Java 带有条目(BLOB)的Java上传zip到Oracle - Java upload zip with entries (BLOB) to Oracle 如何在没有 FileInputStream 或文件路径作为字符串的情况下从 java.sql.Blob 类型的 zip 文件中读取和提取 zip 条目 - How to read and extract zip entries from java.sql.Blob type zip file without having FileInputStream or filepath as a string java 如何访问具有blob协议的PDF文件,java.net.MalformedURLException:未知协议:blob如何在selenium java中解决? - How to access the PDF file having blob protocol, java.net.MalformedURLException: unknown protocol: blob how to resolve in selenium java? 使用Java将多个pdf压缩为单个文件zip文件 - Zip multiple pdf into a single file zip file using Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM