邮政编码相当简单,但我在将 ZipInputStream 作为 Inputstream 返回时遇到了问题。出于某种原因,zip 中包含的某些文件的字符被丢弃。以下是我的解决方案,到目前为止它一直在工作。
private Map<String, InputStream> getFilesFromZip(final DataHandler dhZ,
String operation) throws ServiceFault
{
Map<String, InputStream> fileEntries = new HashMap<String, InputStream>();
try
{
DataSource dsZ = dhZ.getDataSource();
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;
}