java gzip xml,java gzip无法保留原始文件的扩展名

I'm using GZIPOutputStream to gzip one xml file to gz file, but after zipping I find the extension name of the xml file (.xml) is missing in the gz file hierarchy. I need to keep the extension name because the zipped gz file will be used by third party system which expects getting a .xml file after unzipping gz file. Are there any solutions for this? My test code is:

public static void main(String[] args) {

compress("D://test.xml", "D://test.gz");

}

private static boolean compress(String inputFileName, String targetFileName){

boolean compressResult=true;

int BUFFER = 1024*4;

byte[] B_ARRAY = new byte[BUFFER];

FileInputStream fins=null;

FileOutputStream fout=null;

GZIPOutputStream zout=null;

try{

File srcFile=new File(inputFileName);

fins=new FileInputStream (srcFile);

File tatgetFile=new File(targetFileName);

fout = new FileOutputStream(tatgetFile);

zout = new GZIPOutputStream(fout);

int number = 0;

while((number = fins.read(B_ARRAY, 0, BUFFER)) != -1){

zout.write(B_ARRAY, 0, number);

}

}catch(Exception e){

e.printStackTrace();

compressResult=false;

}finally{

try {

zout.close();

fout.close();

fins.close();

} catch (IOException e) {

e.printStackTrace();

compressResult=false;

}

}

return compressResult;

}

解决方案

Not sure what the problem is here, you are calling your own compress function

private static boolean compress(String inputFileName, String targetFileName)

with the following arguments

compress("D://test.xml", "D://test.gz");

Quite obviously you are going to lose the .xml portion of the filename, you never pass it into your method.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值