我有一个方法可以压缩 5 个文件。它生成一个 zip 文件,没有错误,但我无法打开它来检查内容。我尝试通过电子邮件发送它,但 gmail 说它无法发送损坏的文件。尝试在 Windows 中使用 WinRAR 打开会出现错误:
The archive is either in unknown format or damaged
方法是这样的:
private void zipTestFiles() throws FileNotFoundException, IOException
{
File[] filenames = fileDir.listFiles(fileNameFilter(Constants.PAGE_MON_FILENAME_FILTER));
byte[] buf = new byte[1024];
String outFilename = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separatorChar + Constants.PAGEMONITOR_ZIP;
DeflaterOutputStream out = new DeflaterOutputStream(new BufferedOutputStream(new FileOutputStream(outFilename)));
for (int i=0; i<filenames.length; i++)
{
FileInputStream in = new FileInputStream(filenames[i]);
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
in.close();
}
out.close();
}
最佳答案
您应该使用ZipOutputStream
而不是DeflaterOutputStream
。并且不要忘记创建条目。在编写实现之前,请阅读 ZipOutputStream 的 javadoc。
关于java - 这种 zip 方法有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8328044/