Java 将文件压缩为InputStream,然后分离其中的每个文件,然后将其转换为图像。在爪哇
我得到一个zip文件作为Java 将文件压缩为InputStream,然后分离其中的每个文件,然后将其转换为图像。在爪哇,java,zip,bytearray,inputstream,pdfbox,Java,Zip,Bytearray,Inputstream,Pdfbox,我得到一个zip文件作为InputStream。然后将其中的每个文件分开。然后我将相同的字节数组传递给一个内部使用Apace pdf box 1.6.0将其转换为图像的 但是,当我将字节数组传递给PDFDocumentReader时,我得到以下异常- SEVERE: expected='endstream' actual='' org.apache.pdfbox.io.PushBackInputStream@44c2beb9 java.io.IOException: expected='ends
InputStream
。然后将其中的每个文件分开。然后我将相同的字节数组
传递给一个内部使用Apace pdf box 1.6.0
将其转换为图像的
但是,当我将字节数组
传递给PDFDocumentReader
时,我得到以下异常-
SEVERE: expected='endstream' actual='' org.apache.pdfbox.io.PushBackInputStream@44c2beb9
java.io.IOException: expected='endstream' actual='' org.apache.pdfbox.io.PushBackInputStream@44c2beb9
at org.apache.pdfbox.pdfparser.BaseParser.parseCOSStream(BaseParser.java:439)
at org.apache.pdfbox.pdfparser.PDFParser.parseObject(PDFParser.java:530)
at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:172)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:862)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:829)
at org.dopdf.document.read.pdf.PDFDocumentReader.init(PDFDocumentReader.java:98)
要从zip中获取每个文件,我使用以下代码-
ZipInputStream zis = new ZipInputStream(aZipFile); // aZipFile is byte array
ZipEntry entry;
ArrayList<String> nameOfIgnoredFiles = new ArrayList<String>();
byte data[] = null;
while ((entry = zis.getNextEntry()) != null) {
if (entry.getName().endsWith(".pdf")) {
int dataSize = (int)entry.getSize();
data = new byte[dataSize];
zis.read(data);
// i use data and pass it to the pdf box.
} else {
nameOfIgnoredFiles.add(entry.getName());
}
我做错了什么?你能提出一个解决方案吗?我想数据字节数组的获取是一个问题。如何以最佳方式执行此操作?您假设
zis.read(data)
会填充缓冲区。检查API文档。这并不能保证做到这一点。您还假设大小适合int,并且项本身适合内存。这些假设都是无效的
当然,您可以将条目的
InputStream
传递给pdfbox
API?@Juniad(a)在循环中读取,直到您获得所需的所有数据,这对我来说是完全显而易见的,或者(b)使用DataInputStream.readFully()
,或者(c)我上面最后一句话中包含的更好的建议。1。我试图在循环中读取它,但是在组合字节数组时遇到了一些问题。2. DataInputStream
在其构造函数中需要一个InputStream
。通过哪一个?3.可以通过InputStream
但是,当我将文件保存在HBase
中时,我必须将其转换为字节数组
,所以之前必须这样做。@Juniad(a)zis
当然;(b) 如果不需要输入流
,将zis
复制到ByteArrayOutputStream
中,并从中提取字节。我可以将zis传递到DataInputStream
中,但如何将压缩在作为流接收的zip文件中的文件分离开来。@JUIAD在每个项目末尾读取输入流时,您将获得EOF。然后进入下一项并再次阅读。如果没有其他项目,请停止。这些都在Javadoc中。
PDFDocumentReader document = new PDFDocumentReader(data); // here i get the error