我正在尝试以编程方式解压缩 .xml.gz 文件。这似乎非常简单,因为互联网上有许多示例可以告诉您如何解压缩 .gz 文件。但是,每次我尝试这样做时,都会出现异常:java.io.IOException: unknown format (magic number d4d4)。
我正在尝试在 Android 应用程序中执行此操作,是否应该采用与在 Java 中不同的方式来执行此操作?
我正在关注可用的示例代码 here .
有人知道我在这里做错了什么吗?另外,我在从网络服务器下载文件后解压缩文件。下载似乎运行良好。
最佳答案
找到一个下载的同时解压的好办法。工作起来轻而易举。
protected String doInBackground(String... sUrl) {
try {
URL url = new URL(sUrl[0]);
URLConnection connection = url.openConnection();
InputStream stream = connection.getInputStream();
stream = new GZIPInputStream(stream);
InputSource is = new InputSource(stream);
InputStream input = new BufferedInputStream(is.getByteStream());
OutputStream output = new FileOutputStream("Path to the file");
byte data[] = new byte[2097152];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (BufferOverflowException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
引用:http://abhirampal.com/2012/05/17/android-download-decompress-gzip/
关于Android解压下载的.xml.gz文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10469407/