[英]Android unzip open failed: ENOENT (No such file or directory)
I am using the following code to unzip a set of files (containing folders as well): 我正在使用以下代码解压缩一组文件(也包含文件夹):
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;
}
The code fails on FileOutputStream fout = new FileOutputStream(path + filename) with the error: 代码在FileOutputStream fout = new FileOutputStream(path + filename)上失败,并显示以下错误:
java.io.FileNotFoundException: /storage/emulated/0/BASEFOLDER/FOLDER1/FILE.png
BASEFOLDER already exists, that is where I am trying to unzip the folder to. BASEFOLDER已经存在,这是我尝试将文件夹解压缩到的位置。 If I manually (or programmatically) create FOLDER1, the code runs fine and successfully unzips. 如果我手动(或以编程方式)创建FOLDER1,则代码可以正常运行并成功解压缩。 I believe it is crashing because the very first file (ze) is named FOLDER1/FILE.png and FOLDER1 hasn't been created yet. 我相信它会崩溃,因为第一个文件(ze)名为FOLDER1 / FILE.png,而FOLDER1尚未创建。 How do I get around this? 我该如何解决? I know other people have used this code, I find it unlikely that it randomly doesn't work for me... 我知道其他人已经使用了此代码,但我发现它不太可能对我不起作用...
Have you got this in your AndroidManifest.xml file? 您在AndroidManifest.xml文件中找到了吗?
Add Write to external Storage permission 将写入添加到外部存储权限
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 使用权限android:name =“ android.permission.WRITE_EXTERNAL_STORAGE”
I had the same problem. 我有同样的问题。 After several investigation I found that. 经过几次调查,我发现了。 put following single line in your code: 在代码中添加以下一行:
if (ze.isDirectory()) {
File fmd = new File(path + filename);
fmd.mkdirs();
zis.closeEntry(); // <<<<<< ADD THIS LINE
continue;
}
Sometime the extract files has been extracted before its parent directory is created, for example: File A inside directory B. But B directory is not created, index of files listing below cause the issue: 有时,提取文件已在创建其父目录之前被提取,例如:目录B内部的文件A。但是未创建目录B,下面列出的文件索引会导致问题:
So, to sure directory created before file extracting, you need to create parent directories first, for example: 因此,要确保在提取文件之前已创建目录,则需要首先创建父目录,例如:
val targetFile = File(tempOutputDir, zipEntry.name)
if (zipEntry.isDirectory) {
targetFile.mkdirs()
} else {
try {
try {
targetFile.parentFile?.mkdirs() // <-- ADD THIS LINE
} catch (exception: Exception) {
Log.e("ExampleApp", exception.localizedMessage, exception)
}
val bufferOutputStream = BufferedOutputStream(
FileOutputStream(targetFile)
)
val buffer = ByteArray(1024)
var read = zipInputStream.read(buffer)
while (read != -1) {
bufferOutputStream.write(buffer, 0, read)
read = zipInputStream.read(buffer)
}
bufferOutputStream.close()
} catch (exception: Exception) {
Log.e("ExampleApp", exception.localizedMessage, exception)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.