我正在下载一个zip文件并将其保存在我的安卓设备的特定文件夹中。由于文件被压缩,我的应用程序无法访问该文件夹。我想在从服务器下载后解压缩文件夹并保存到指定文件夹中。
这是我的代码:
这是我的代码:
public void DownloadDatabase(String DownloadUrl, String fileName) {
try {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/timy/databases");
if(dir.exists() == false){
dir.mkdirs();
}
URL url = new URL("http://myexample.com/android/timy.zip");
File file = new File(dir,fileName);
long startTime = System.currentTimeMillis();
Log.d("DownloadManager" , "download url:" +url);
Log.d("DownloadManager" , "download file name:" + fileName);
URLConnection uconn = url.openConnection();
uconn.setConnectTimeout(TIMEOUT_SOCKET);
InputStream is = uconn.getInputStream();
ZipInputStream zipinstream = new ZipInputStream(new BufferedInputStream(is));
ZipEntry zipEntry;
while((zipEntry = zipinstream.getNextEntry()) != null){
String zipEntryName = zipEntry.getName();
File file1 = new File(file + zipEntryName);
if(file1.exists()){
}else{
if(zipEntry.isDirectory()){
file1.mkdirs();
}
}
}
BufferedInputStream bufferinstream = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(5000);
int current = 0;
while((current = bufferinstream.read()) != -1){
baf.append((byte) current);
}
FileOutputStream fos = new FileOutputStream( file);
fos.write(baf.toByteArray());
fos.flush();
fos.close();
Log.d("DownloadManager" , "download ready in" + ((System.currentTimeMillis() - startTime)/1000) + "sec");
}
catch(IOException e) {
Log.d("DownloadManager" , "Error:" + e);
e.printStackTrace();
}
}
我的logcat显示出现错误。只有文件夹在我的设备上创建,没有任何文件被下载并解压缩。如果不使用inputZipStream方法,则我的压缩文件夹会下载并保存在sdcard中。但是当我想要解压缩它时,却没有发生任何事情。