我需要在Apache中读取和处理中的zip归档中的特定文件。
在文档中,我发现
Flink目前支持透明解压缩输入文件,如果这些文件标记为适当的文件扩展名。
https://ci.apache.org/projects/flink/flink-docs-release-1.4/dev/batch/#read-compressed-files
是否有可能在Apache中动态解压缩时处理它?
发布于 2018-03-08 04:56:47
同时,我想分享我实现的解决方案。
因此,在创建自己的InputFormat之后,我在open()方法中使用了以下代码:
@Override
public void open(final FileInputSplit ignored) throws IOException {
...
final XMLInputFactory xmlif = XMLInputFactory.newInstance();
final XMLStreamReader xmlr = xmlif.createXMLStreamReader(filePath.toString(),
InputFormatUtil.readFileWithinZipArchive(filePath, nestedXmlFileName));
while (xmlr.hasNext()) {
...
}其中,readFileWithinZipArchive(...)的实现是:
public static InputStream readFileWithinZipArchive(final Path zipPath, final String filename) throws IOException {
// using org.apache.flink.core.fs.Path for getting the InputStream from the (remote) zip archive
final InputStream zipInputStream = zipPath.getFileSystem().open(zipPath);
// generating a temporary local copy of the zip file
final File tmpFile = stream2file(zipInputStream);
// then using java.util.zip.ZipFile for extracting the InputStream for the specific file within the zip archive
final ZipFile zipFile = new ZipFile(tmpFile);
return zipFile.getInputStream(zipFile.getEntry(filename));
}发布于 2018-03-06 07:01:28
FileInputFormat将将读取的压缩文件委托给GZIPInputStream,后者将在解压缩时返回部分解压缩数据。
https://stackoverflow.com/questions/49122170
复制相似问题