[英]Download zip file using download manager
I want to download a zip file from a particular zip link and then unzip that zip file in android.What should i do? 我想从特定的zip链接下载一个zip文件,然后在android中解压缩该zip文件。我该怎么办? Can I use android download manager?
我可以使用android下载管理器吗?
Yes you can use it, here is a small snippet: 是的,你可以使用它,这是一个小片段:
DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request req = new DownloadManager.Request(Uri.parse("url-of-the-zip"));
req.setDestinationExternalFilesDir(Environment.DIRECTORY_DOWNLOADS, "filename.zip");
long id = dm.enqueue(req);
The id can later be used to request the local Uri of the downloaded file using DownloadManager.getUriForDownloadedFile(int) . 以后可以使用id来使用DownloadManager.getUriForDownloadedFile(int)请求下载文件的本地Uri。 To unzip this file you can use ZipFile
要解压缩此文件,您可以使用ZipFile
Hi download zip file path is show below, 您好下载zip文件路径如下所示,
URL url = new URL("ZIP_FILE");
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
int lenghtOfFile = c.getContentLength();
// Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
Long size = Long.parseLong(String.valueOf(input.available()));
StatFs stat = new StatFs(activity.getParent().getFilesDir().getAbsolutePath());
long freeBytes = ((long)stat.getAvailableBlocks()) * stat.getBlockSize();
// Log.e("TOTAL AVAILABLE", "@"+input.available() + " FREE" + freeBytes);
if(freeBytes >= size){
// String outFilePath = Environment.getExternalStorageDirectory().toString();
String path = outFilePath +"/TEST";
File myNewFolder = new File(path);
if (!myNewFolder.isDirectory()) {
myNewFolder.mkdirs();
}
path = path+"/"+"FileName";
OutputStream output = new FileOutputStream(path);
byte data[] = new byte[1024];
long total = 0;
int count = 0;
publishProgress(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();
//path your save location
}
unzip code is 解压缩代码是
public void unzipAll(File zipFile, File targetDir) throws IOException {
Log.i(TAG, "[METHOD] void unzipAll(zipFile:" + zipFile + ", targetDir:" + targetDir + ")");
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
ZipEntry zentry = null;
// if exists remove
if (targetDir.exists()) {
FileUtils.deleteDirectory(targetDir);
targetDir.mkdirs();
}
else {
targetDir.mkdirs();
}
Log.d(TAG, "targetDir: " + targetDir);
// unzip all entries
while ((zentry = zis.getNextEntry()) != null) {
String fileNameToUnzip = zentry.getName();
File targetFile = new File(targetDir, fileNameToUnzip);
// if directory
if (zentry.isDirectory()) {
(new File(targetFile.getAbsolutePath())).mkdirs();
}
else {
// make parent dir
(new File(targetFile.getParent())).mkdirs();
unzipEntry(zis, targetFile);
Log.d(TAG, "Unzip file: " + targetFile);
}
}
zis.close();
}
private File unzipEntry(ZipInputStream zis, File targetFile) throws IOException {
FileOutputStream fos = new FileOutputStream(targetFile);
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = zis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
return targetFile;
}
hope this code is help you.. i am using this code for downloading and unzip file 希望这段代码能帮到你..我正在使用这段代码下载和解压缩文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.