解压包含子文件夹的文件时出现 FileNotFound
FileNotFound when unzipping file with sub folders
我有一个 UnZip class 可以从数据库中获取图像
class UnZip extends AsyncTask<String, Integer, String> {
private String _mArchivePath;
private String _mOutPutStream;
private int per;
public UnZip(String mArchivePath,String mOutPutStream) {
_mArchivePath = mArchivePath;
_mOutPutStream = mOutPutStream;
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
InputStream inputstream;
ZipInputStream zipinputstream;
try {
String filename;
inputstream = new FileInputStream(_mArchivePath);
zipinputstream = new ZipInputStream(new BufferedInputStream(inputstream));
ZipEntry mZipEntry;
byte[] buffer = new byte[32*1024];
int count;
while ((mZipEntry = zipinputstream.getNextEntry()) != null) {
Log.v("Decompress", "Unzipping " + mZipEntry.getName());
filename = mZipEntry.getName();
per++;
publishProgress(per);
if (mZipEntry.isDirectory()) {
File fmd = new File(_mOutPutStream + filename);
fmd.mkdirs();
continue;
}
FileOutputStream fileoutputstream = new FileOutputStream(_mOutPutStream + filename);
while ((count = zipinputstream.read(buffer)) != -1) {
fileoutputstream.write(buffer, 0, count);
}
fileoutputstream.close();
zipinputstream.closeEntry();
}
zipinputstream.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
它与包含图像 (32MB~) 的 database.zip 一起使用时效果很好,但是当我尝试使用相同的 database.zip 但有一些额外的图像 (43MB~) 时,它给了我这个错误:
03-03 23:42:00.200: V/Decompress(11593): Unzipping /database/weed/ak_47_1.jpg
03-03 23:42:00.202: W/System.err(11593): java.io.FileNotFoundException: /storage/emulated/0/unzipped/database/weed/ak_47_1.jpg: open failed: ENOENT (No such file or directory)
03-03 23:42:00.204: W/InputMethodManagerService(584): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@cce61f3 attribute=android.view.inputmethod.EditorInfo@aa9eeb0, token = android.os.BinderProxy@2db5667
我发现了问题,当我尝试解压缩包含子文件夹的文件时,如果子文件夹不存在,应用程序会给我这个错误。暂时我更改了主要代码,所以当应用程序启动时它会自动创建文件夹,因为我知道 zip 的结构,但是如果我想使用另一个 zip 我该怎么办?
您用来解压 ZIP 的代码似乎有问题,即子文件夹的创建没有按预期工作。
最好的办法是调查一下,正如@BrentM 所建议的,但是如果您赶时间,快速搜索 SO 可能会有帮助:
How to unzip files programmatically in Android?
我使用了该线程中的以下方法,可以确认它运行良好:
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)
{
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);
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;
}
我有一个 UnZip class 可以从数据库中获取图像
class UnZip extends AsyncTask<String, Integer, String> {
private String _mArchivePath;
private String _mOutPutStream;
private int per;
public UnZip(String mArchivePath,String mOutPutStream) {
_mArchivePath = mArchivePath;
_mOutPutStream = mOutPutStream;
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
InputStream inputstream;
ZipInputStream zipinputstream;
try {
String filename;
inputstream = new FileInputStream(_mArchivePath);
zipinputstream = new ZipInputStream(new BufferedInputStream(inputstream));
ZipEntry mZipEntry;
byte[] buffer = new byte[32*1024];
int count;
while ((mZipEntry = zipinputstream.getNextEntry()) != null) {
Log.v("Decompress", "Unzipping " + mZipEntry.getName());
filename = mZipEntry.getName();
per++;
publishProgress(per);
if (mZipEntry.isDirectory()) {
File fmd = new File(_mOutPutStream + filename);
fmd.mkdirs();
continue;
}
FileOutputStream fileoutputstream = new FileOutputStream(_mOutPutStream + filename);
while ((count = zipinputstream.read(buffer)) != -1) {
fileoutputstream.write(buffer, 0, count);
}
fileoutputstream.close();
zipinputstream.closeEntry();
}
zipinputstream.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
它与包含图像 (32MB~) 的 database.zip 一起使用时效果很好,但是当我尝试使用相同的 database.zip 但有一些额外的图像 (43MB~) 时,它给了我这个错误:
03-03 23:42:00.200: V/Decompress(11593): Unzipping /database/weed/ak_47_1.jpg
03-03 23:42:00.202: W/System.err(11593): java.io.FileNotFoundException: /storage/emulated/0/unzipped/database/weed/ak_47_1.jpg: open failed: ENOENT (No such file or directory)
03-03 23:42:00.204: W/InputMethodManagerService(584): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@cce61f3 attribute=android.view.inputmethod.EditorInfo@aa9eeb0, token = android.os.BinderProxy@2db5667
我发现了问题,当我尝试解压缩包含子文件夹的文件时,如果子文件夹不存在,应用程序会给我这个错误。暂时我更改了主要代码,所以当应用程序启动时它会自动创建文件夹,因为我知道 zip 的结构,但是如果我想使用另一个 zip 我该怎么办?
您用来解压 ZIP 的代码似乎有问题,即子文件夹的创建没有按预期工作。
最好的办法是调查一下,正如@BrentM 所建议的,但是如果您赶时间,快速搜索 SO 可能会有帮助:
How to unzip files programmatically in Android?
我使用了该线程中的以下方法,可以确认它运行良好:
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)
{
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);
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;
}