該郵政編碼相當簡單,但是我將ZipInputStream作爲InputStream返回時遇到了問題。由於某些原因,zip中包含的一些文件中有字符被丟棄。下面是我的解決方案,迄今爲止它一直在努力。
private Map<String, InputStream> getFilesFromZip(final DataHandler dhZ,
String operation) throws ServiceFault {
Map<String, InputStream> fileEntries = new HashMap<String, InputStream>();
try {
ZipInputStream zipIsZ = new ZipInputStream(dhZ.getDataSource()
.getInputStream());
try {
ZipEntry entry;
while ((entry = zipIsZ.getNextEntry()) != null) {
if (!entry.isDirectory()) {
Path p = Paths.get(entry.toString());
fileEntries.put(p.getFileName().toString(),
convertZipInputStreamToInputStream(zipIsZ));
}
}
}
finally {
zipIsZ.close();
}
} catch (final Exception e) {
faultLocal(LOGGER, e, operation);
}
return fileEntries;
}
private InputStream convertZipInputStreamToInputStream(
final ZipInputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copy(in, out);
InputStream is = new ByteArrayInputStream(out.toByteArray());
return is;
}
我不明白你的意思嗎?你可以添加一個代碼示例嗎? – Whitecat 2013-01-31 17:08:22
他的意思是ZipInputStream既可以用作整個zip,也可以用於讀取每個組件。 .getNextEntry()進入第一個組件,讀取它,做另一個.getNextEntry(),你的流重置爲第二個組件,等等。實際上很聰明。 – akauppi 2014-08-07 07:21:43