Jér*_*nge 3 java resources gzip loading
我正在尝试从我的.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 );
}
Run Code Online (Sandbox Code Playgroud)
编辑
两个"文件"都具有相同的内容.
编辑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);
}
Run Code Online (Sandbox Code Playgroud)
我得到:
Total file: 9132744
Total resource: 16399085
Run Code Online (Sandbox Code Playgroud)
很奇怪 !!
编辑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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4746 次 |
| 最近记录: |