java.io.IOException:不是带有 class.getResourceAsStream() 的 GZIP 格式

发布于 2024-11-24 16:57:06 字数 2361 浏览 2 评论 0原文

我试图从 .jar 中的资源加载一些 GZIP 数据,但收到一条 java.io.IOException: Not in GZIP 格式消息。

当我从文件加载相同的数据时,我没有收到任何错误。为什么? (这是我用 NetBeans 编译的一个 Maven 项目)

以下是生成问题的测试代码:

public static void main(String[] args) throws IOException {

    byte[] dummy = new byte[10];

    // Reading data from file
    File f = new File("C:\\Temp\\422\\convert1900.data");
    DataInputStream is = new DataInputStream(
            new GZIPInputStream(new FileInputStream(f)));

    while ( is.read(dummy) != -1 );

    // Reading data from resource
    InputStream ins = CompareTest2.class.getResourceAsStream(
            "/net/cv/convert1900.data");

    is = new DataInputStream(
        new GZIPInputStream(ins)); // Issue happens here

    while ( is.read(dummy) != -1 );

}

编辑

两个“文件”具有相同的内容。

编辑2

我只是尝试使用以下代码来计算两种方法获得的字节数:

public static void main(String[] args) throws IOException {

    int total = 0;
    int retr = 0;

    byte[] dummy = new byte[10];

    // Reading data from file
    File f = new File("C:\\Temp\\422\\convert1900.data");
    InputStream is = new FileInputStream(f);

    do {
        retr = is.read(dummy);
        if ( retr != -1 )
            total += retr;
    } while ( retr != -1 );

    System.out.println("Total file: " + total);

    // Reading data from resource
    InputStream ins = CompareTest2.class.getResourceAsStream(
            "/net/cv/convert1900.data");

    total = 0;
    retr = 0;

    do {
        retr = ins.read(dummy);
        if ( retr != -1 )
            total += retr;
    } while ( retr != -1 );

    System.out.println("Total resource: " + total);

}

我得到:

Total file: 9132744
Total resource: 16399085

非常奇怪!

编辑3

我刚刚注意到.jar中资源的大小是16399085(未压缩)而不是9132744。看来问题出在编译过程中。

可能我的 pom.xml 中存在以下问题:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <encoding>${project.build.sourceEncoding}</encoding> // UTF8
            </configuration>
        </plugin>

I am trying to load some GZIP-ed data from a resource in my .jar, but I get a java.io.IOException: Not in GZIP format message.

When I load the same data from a file, I don't get any error. Why? (It is a maven project I compile with NetBeans)

Here is the test code generating the issue:

public static void main(String[] args) throws IOException {

    byte[] dummy = new byte[10];

    // Reading data from file
    File f = new File("C:\\Temp\\422\\convert1900.data");
    DataInputStream is = new DataInputStream(
            new GZIPInputStream(new FileInputStream(f)));

    while ( is.read(dummy) != -1 );

    // Reading data from resource
    InputStream ins = CompareTest2.class.getResourceAsStream(
            "/net/cv/convert1900.data");

    is = new DataInputStream(
        new GZIPInputStream(ins)); // Issue happens here

    while ( is.read(dummy) != -1 );

}

EDIT

Both 'files' have the same content.

EDIT 2

I just tried to count the number of bytes I get with both methods using the following code:

public static void main(String[] args) throws IOException {

    int total = 0;
    int retr = 0;

    byte[] dummy = new byte[10];

    // Reading data from file
    File f = new File("C:\\Temp\\422\\convert1900.data");
    InputStream is = new FileInputStream(f);

    do {
        retr = is.read(dummy);
        if ( retr != -1 )
            total += retr;
    } while ( retr != -1 );

    System.out.println("Total file: " + total);

    // Reading data from resource
    InputStream ins = CompareTest2.class.getResourceAsStream(
            "/net/cv/convert1900.data");

    total = 0;
    retr = 0;

    do {
        retr = ins.read(dummy);
        if ( retr != -1 )
            total += retr;
    } while ( retr != -1 );

    System.out.println("Total resource: " + total);

}

and I get:

Total file: 9132744
Total resource: 16399085

Very strange !!

EDIT 3

I have just noticed the size of the resource in the .jar is 16399085 (uncompressed) instead of 9132744. It seems that the issue is in the compilation process.

May be I have an issue with the following in my pom.xml:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <encoding>${project.build.sourceEncoding}</encoding> // UTF8
            </configuration>
        </plugin>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谈下烟灰 2024-12-01 16:57:06

我的问题在于我的资源的过滤。 此处提供了解决方案。

My issue lied in the filtering of my resources. A solution is available here.

只为一人 2024-12-01 16:57:06

我编译了你的代码,无法重现你的情况。我的代码片段如下。

尝试断言您通过 getResourceAsStream 获取了正确的数据 - 例如,将其转储到没有 un-gzip 的文件中,并使用文件方法重新加载它。
可能是 /net/cv/convert... 在类路径中可见两次,而运行时得到了错误的结果?

java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03-384-10M3425)
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02-384, mixed mode)

javac A.java 
java -cp mega.jar:. A

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.IllegalStateException;
import java.util.zip.GZIPInputStream;

public class A {
public static void main(String[] args) throws IOException {
    final byte[] dummy = new byte[10];

    // Reading data from file
    final File f = new File("/tmp/mine.data");
    DataInputStream is = new DataInputStream(new GZIPInputStream(new FileInputStream(f)));

    int count = 0;
    while (is.read(dummy) != -1) count++;
    System.out.println("count = " + count);

    // Reading data from resource
    InputStream ins = A.class.getResourceAsStream("/do/do/mine.data");
    if (ins == null)
        throw new IllegalStateException("Failed to locate data.");

    is = new DataInputStream(new GZIPInputStream(ins)); // Issue happens here
    count = 0;
    while (is.read(dummy) != -1) count++;

    System.out.println("count = " + count);
}
}

I compiled your code and can't reproduce your situation. My code snippet follows.

Try to assert that you get proper data thru getResourceAsStream - for example dump it to a file without un-gzip and reload it using the File approach.
Might be you have the /net/cv/convert... visible twice in your classpath and the runtime gets the wrong one?

java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03-384-10M3425)
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02-384, mixed mode)

javac A.java 
java -cp mega.jar:. A

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.IllegalStateException;
import java.util.zip.GZIPInputStream;

public class A {
public static void main(String[] args) throws IOException {
    final byte[] dummy = new byte[10];

    // Reading data from file
    final File f = new File("/tmp/mine.data");
    DataInputStream is = new DataInputStream(new GZIPInputStream(new FileInputStream(f)));

    int count = 0;
    while (is.read(dummy) != -1) count++;
    System.out.println("count = " + count);

    // Reading data from resource
    InputStream ins = A.class.getResourceAsStream("/do/do/mine.data");
    if (ins == null)
        throw new IllegalStateException("Failed to locate data.");

    is = new DataInputStream(new GZIPInputStream(ins)); // Issue happens here
    count = 0;
    while (is.read(dummy) != -1) count++;

    System.out.println("count = " + count);
}
}
玩心态 2024-12-01 16:57:06

我非常确定 InputStream ins = CompareTest2.class.getResourceAsStream("/net/cv/convert1900.data"); 行返回 null。检查一下。如果是这样,请检查您的通行证/net/cv/convert1900.data,检查您的jar,在从maven运行时验证类路径:可能资源不存在。

顺便说一句,这是可能的! maven中的所有资源都必须在resources目录下。它适合您的项目吗?例如,如果资源文件位于 main/java 下,它们将不会被复制到目标目录。

I am pretty sure that line InputStream ins = CompareTest2.class.getResourceAsStream("/net/cv/convert1900.data"); returns null. Check this. If so check your pass /net/cv/convert1900.data, check your jar, verify the classpath when running from maven: probably the resource is not there.

BTW it is possible! All resources in maven must be under directory resources. Is it correct for your project? If for example resource files are under main/java they will not be copied to target directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文