使用下载管理器下载zip文件

4

我想从特定的zip链接下载一个zip文件,然后在安卓设备上解压缩这个zip文件。我应该怎么做?我能使用安卓下载管理器吗?

2个回答

9

是的,您可以使用它,这里是一个小片段:

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);

使用DownloadManager.getUriForDownloadedFile(int)可以请求已下载文件的本地Uri。要解压缩这个文件,您可以使用ZipFile


我尝试了这个。它显示下载通知。但它不在外部目录中。 - Anu
你说得对,我重新阅读了文档,发现不能将null传递给setDestinationExternalFilesDir。我会编辑答案,这可能会解决你的问题。 - pshegger
1
Context.DOWNLOAD-SERVICE > Context.DOWNLOAD_SERVICE - Khaled Lela

6

您好,压缩文件下载路径如下:

                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

                }

解压代码是

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;
    }

希望这段代码能对您有所帮助。我正在使用这段代码来下载和解压文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接