使用其他PDF文件在zip文件中附加XML文件

时间:2013-04-09 03:55:46

标签: java xml zip

我有一个xml文档的输出流。我将所有PDF文件附加在一个zip文件中。一旦我完成了附加所有PDF文件,我想附加xml文档。但我得到的只是空的xml文件。

这就是我正在做的事情

byte b[] = new byte[10240];
ZipOutputStream zout = new ZipOutputStream(getOutputStream("ExampleForms.zip", attach));
for(int i = 0; i < pdfs.length; i++){
    File f = File.createTempFile(pdfs[i].NAME, ".pdf");
    FileOutputStream fo = new FileOutputStream(f);
    pdfs[i].render(fo);
    fo.close();
    InputStream in = new FileInputStream(f);
    ZipEntry e = new ZipEntry(pdfs[i].NAME + ".pdf");
    zout.putNextEntry(e);
    int len=0;
    while((len=in.read(b)) != -1) {
          zout.write(b,0,len);
    }
    in.close();
    zout.closeEntry();
    f.delete();
}       
/* out is my outputstream in which i have written xml document */
File f = File.createTempFile("SampleXmlFile.xml");
FileOutputStream fo = new FileOutputStream(f);
fo.write(out.toString().getBytes());
fo.close();
InputStream is = new FileInputStream(f);
ZipEntry e = new ZipEntry("MyXmlFile",".xml");
zout.putNextEntry(e);
int lent = 0;
while((lent = is.read(bt)) != -1){
   zout.write(bt,0,lent);
}
is.close();
zout.closeEntry();
zout.close();

1 个答案:

答案 0 :(得分:1)

1)概率out是空的

2)为什么要使用临时文件?使用

 zout.write(out.toString().getBytes());