我正在尝试向现有代码添加内容,因此我将继续使用项目所具有的结构。
要创建输出zip文件,代码具有以下流程:
String bakecaPath = configurationManager.getProviderParam(CommonConstants.THIRD_PARTY_PROVIDER_NAME, CommonConstants.BAKECA_FEED_XML_PATH);
File bakecaOutputFile = new File(bakecaPath);
bakecaStream = new ZipOutputStream(new FileOutputStream(bakecaOutputFile));
innerFileName = bakecaOutputFile.getName().replace(".zip", ".xml");
ze = new ZipEntry(innerFileName);
bakecaStream.putNextEntry(ze);
bakecaWriter = xmlOutputFactory.createXMLStreamWriter(bakecaStream, UTF_8);
Marshaller bakecaMarshaller = constructMarshaller(BakecaAd.class);
bakecaWriter.writeStartDocument(UTF_8,"1.0");
bakecaWriter.writeStartElement(getRootElementName(Bakeca.class));
bakecaWriter.writeEndElement();
bakecaWriter.writeEndDocument();
bakecaStream.closeEntry();
closeWriter(bakecaWriter);
其中costructMarshaller是我在此类中使用的方法。
现在,我想创建2个文件.xml(即newFeed.xml和newFeedAgency.xml)并将它们放入一个唯一的文件fcowrnfo.zip(出于安全原因,名称就像这样)
我试着用相同的东西写2次,只有1个servlet,但显然在文件中.zip我只找到了我要编写的第二个文件。
在文件web.xml中:
<servlet>
<servlet-name>BakecaServlet</servlet-name>
<servlet-class>it.antlia.wikire.servlet.BakecaServlet</servlet-class>
<async-supported>true</async-supported>
<servlet-mapping>
<servlet-name>BakecaServlet</servlet-name>
<url-pattern>/bakeca_feed.zip</url-pattern>
如果我制作2个文件编写者,我如何将它们附加到zipStream?
或者更好,我该如何解决这个问题?
答案 0 :(得分:0)
您可以在创建完第一个文件后立即在zip中创建第二个文件。
String bakecaPath = configurationManager.getProviderParam(CommonConstants.THIRD_PARTY_PROVIDER_NAME, CommonConstants.BAKECA_FEED_XML_PATH);
File bakecaOutputFile = new File(bakecaPath);
bakecaStream = new ZipOutputStream(new FileOutputStream(bakecaOutputFile));
innerFileName = bakecaOutputFile.getName().replace(".zip", ".xml");
ze = new ZipEntry(innerFileName);
bakecaStream.putNextEntry(ze);
bakecaWriter = xmlOutputFactory.createXMLStreamWriter(bakecaStream, UTF_8);
Marshaller bakecaMarshaller = constructMarshaller(BakecaAd.class);
bakecaWriter.writeStartDocument(UTF_8,"1.0");
bakecaWriter.writeStartElement(getRootElementName(Bakeca.class));
bakecaWriter.writeEndElement();
bakecaWriter.writeEndDocument();
bakecaStream.closeEntry();
closeWriter(bakecaWriter);
// create the next file
ZipEntry anotherFile = new ZipEntry("filename");
bakecaStream.putNextEntry(anotherFile);
// write to the second file here; you can write to bakecaStream again
bakecaStream.closeEntry();