我正在尝试从.jar中的资源加载一些GZIP-ed数据,但我收到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>
答案 0 :(得分:5)
我的问题在于过滤我的资源。有一个解决方案here。
答案 1 :(得分:2)
我编译了您的代码,无法重现您的情况。我的代码片段如下。
尝试断言通过getResourceAsStream获取正确的数据 - 例如,将其转储到没有un-gzip的文件中,然后使用File方法重新加载它。 您可能在类路径中看到/ 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);
}
}
答案 2 :(得分:1)
我很确定第InputStream ins = CompareTest2.class.getResourceAsStream("/net/cv/convert1900.data");
行返回null。检查一下。如果是这样,请检查您的传递/net/cv/convert1900.data
,检查您的jar,从maven运行时验证类路径:可能资源不在那里。