简体   繁体   English

如何通过带有相对路径的getResourceAsStream读取zip文件?

[英]How to read zip file through getResourceAsStream with relative path?

I used to use this method to read text files in my maven's resources/ directory, so that I can use relative path: 我曾经使用此方法读取Maven的resources /目录中的文本文件,以便可以使用相对路径:

public static BufferedReader fileReaderAsResource(String filePath) throws IOException {
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
        if (is == null) {
            throw new FileNotFoundException(" Not found: " + filePath);
        }
        return new BufferedReader(new InputStreamReader(is, DEFAULT_ENCODING));
    }

Now I need to read zip file due to its size and I still want to use relative path to file in my "resources" directory. 现在,由于大小原因,我需要阅读zip文件,并且我仍想使用相对路径来在“ resources”目录中进行归档。 Is there a way to do this? 有没有办法做到这一点?

I have this method to read zip file, but it only reads in file through absolute path: 我有这种方法来读取zip文件,但它只能通过绝对路径读取文件:

public static BufferedReader fileZipReader(String fileName) throws IOException, URISyntaxException {
        URL zipUrl = IOUtils.class.getClassLoader().getResource(fileName);
        File zipFile = new File(zipUrl.toURI());
        ZipFile zip = new ZipFile(zipFile);
        for (Enumeration e = zip.entries(); e.hasMoreElements(); ) {
            ZipEntry zipEntry = (ZipEntry) e.nextElement();
            if (!zipEntry.isDirectory()) {
                return new BufferedReader(new InputStreamReader(zip.getInputStream(zipEntry)));
            }
        }
        throw new FileNotFoundException("File not found: " + fileName);
    }

How to read zip file through relative path to my standard maven's resources/ directory? 如何通过我的标准Maven的resources /目录的相对路径读取zip文件?

You can wrap an InputStream with a ZipInputStream, ie : 您可以使用ZipInputStream包装InputStream,即:

InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
if (is == null) {
    throw new FileNotFoundException(" Not found: " + filePath);
}
ZipInputStream zis = new ZipInputStream(is);

EDIT: 编辑:

Use the method above named as "fileReaderZipAsResource", I read the file normally: 使用上面命名为“ fileReaderZipAsResource”的方法,我正常读取文件:

try {
            BufferedReader br = fileReaderZipAsResource(qaFilePath);
            String line;
            while ((line = br.readLine()) != null) {
                if (line.isEmpty()) {
                    throw new RuntimeException("Invalid entry ... 2");
                }
                line = line.trim();
                textKGKB.add(line);
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

But the debugging shows that the program doesn't enter the loop. 但是调试显示该程序没有进入循环。 It simply pass by the loop and continue the logic, without throwing an exception either. 它只是简单地通过循环并继续逻辑,而不会抛出异常。 My text file is a 4-column text file delimited with tab key. 我的文本文件是一个4列的文本文件,以Tab键分隔。 I simply zip it and name it as xyy.zip, and pass it as parameter to the method above. 我只是将其压缩并命名为xyy.zip,然后将其作为参数传递给上述方法。

What's the problem? 有什么问题? Does the wrapping of ZipInputStream really work? ZipInputStream的包装真的有效吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM