我正在使用以下代码解压缩一组文件(也包含文件夹):
private boolean unpackZip(String path, String zipname)
{
InputStream is;
ZipInputStream zis;
try
{
String filename;
is = new FileInputStream(path + zipname);
zis = new ZipInputStream(new BufferedInputStream(is));
ZipEntry ze;
byte[] buffer = new byte[1024];
int count;
while ((ze = zis.getNextEntry()) != null)
{
// zapis do souboru
filename = ze.getName();
// Need to create directories if not exists, or
// it will generate an Exception...
if (ze.isDirectory()) {
File fmd = new File(path + filename);
fmd.mkdirs();
continue;
}
FileOutputStream fout = new FileOutputStream(path + filename);
// cteni zipu a zapis
while ((count = zis.read(buffer)) != -1)
{
fout.write(buffer, 0, count);
}
fout.close();
zis.closeEntry();
}
zis.close();
}
catch(IOException e)
{
e.printStackTrace();
return false;
}
return true;
}
FileOutputStream fout = new FileOutputStream(path + filename) 上的代码失败并出现错误:
java.io.FileNotFoundException: /storage/emulated/0/BASEFOLDER/FOLDER1/FILE.png
BASEFOLDER 已经存在,这就是我试图将文件夹解压缩到的位置。如果我手动(或以编程方式)创建 FOLDER1,代码运行良好并成功解压缩。我相信它正在崩溃,因为第一个文件(ze)被命名为 FOLDER1/FILE.png 并且 FOLDER1 尚未创建。我该如何解决这个问题?我知道其他人已经使用过这个代码,我发现它不太可能随机地对我不起作用......