我正在从服务器下载zip文件到我的内部存储。它保存到/data/data/[package name]/files
目录,我可以使用Android Studio中的设备文件资源管理器查看它。但是当我尝试访问它以解压缩时,我收到以下错误:
java.io.FileNotFoundException: thezip.zip: open failed: ENOENT (No such file or directory)
我打电话的时候:
context.getFileDir();
它给了我以下目录:
/data/user/0/[package name]/files
这是
的符号链接/data/data/[package name]/files directory
所以我应该可以访问zip文件,不应该吗?
我在这里缺少什么?我希望能够让任何使用该应用程序的用户访问zip文件夹中的文件。救命啊!
编辑:这是代码:
class DownloadFileAsync extends AsyncTask<String, String, String> {
private Context context;
private String fileName;
public DownloadFileAsync(Context context, String fileName){
this.context = context;
this.fileName = fileName;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... aurl) {
downloadZipFile(aurl[0]);
try {
unzipFolder(fileName);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
}
@Override
protected void onPostExecute(String unused) {
}
private void downloadZipFile(String link){
int count;
try {
URL url = new URL(link);
URLConnection connection = url.openConnection();
connection.connect();
int lenghtOfFile = connection.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
// File file = new File(context.getFilesDir(), fileName);
File file = new File(context.getFilesDir(), fileName);
Log.d("zzzz", file.toString());
InputStream input = new BufferedInputStream(url.openStream());
FileOutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
}
private void unzipFolder(String zippedFile) throws IOException {
//Log.d("zzzz", context.getFilesDir().toString());
byte[] buffer = new byte[1024];
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zippedFile));
ZipEntry zipEntry = zipInputStream.getNextEntry();
while (zipEntry != null){
String aFile = zipEntry.getName();
File newFile = new File(context.getFilesDir(), aFile);
// File newFile = new File(context.getFilesDir(), aFile);
FileOutputStream fileOutputStream = new FileOutputStream(newFile);
int len;
while ((len = zipInputStream.read(buffer)) > 0){
fileOutputStream.write(buffer, 0, len);
}
fileOutputStream.close();
zipEntry = zipInputStream.getNextEntry();
}
zipInputStream.closeEntry();
zipInputStream.close();
}
}