Java URL Download downloadAndUnzip(String url, File location)

Here you can find the source of downloadAndUnzip(String url, File location)

Description

download And Unzip

License

Open Source License

Declaration

public static void downloadAndUnzip(String url, File location) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;

import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    final static String ARCHIVE_DOWNLOAD_NAME = "/download.zip";

    public static void downloadAndUnzip(String url, File location) {
        InputStream in = null;//from  w ww  . j a  v  a2s  . c om
        FileOutputStream out = null;
        File archive = null;
        try {
            new File(location.getCanonicalPath()).mkdirs();
            String archivePath = location.getCanonicalPath()
                    + ARCHIVE_DOWNLOAD_NAME;
            archive = new File(archivePath);
            URL website = new URL((String) url);
            in = website.openStream();
            out = new FileOutputStream(archivePath);
            final byte[] data = new byte[1024]; // 1 KB data
            int count;
            while ((count = in.read(data)) != -1) {
                out.write(data, 0, count);
            }
            unzipArchive(archive, location);
        } catch (Exception e) {
            location.delete();
            throw new RuntimeException(e);
        } finally {
            try {
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (archive != null) {
                archive.delete();
            }
        }
    }

    @SuppressWarnings("rawtypes")
    private static void unzipArchive(File archive, File outputDir)
            throws IOException {
        ZipFile zipfile = new ZipFile(archive);
        for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
            ZipEntry entry = ((ZipEntry) e.nextElement());
            unzipEntry(zipfile, entry, outputDir);
        }
        zipfile.close();
    }

    private static void unzipEntry(ZipFile zipfile, ZipEntry entry,
            File outputDir) throws IOException {
        File outputFile = new File(outputDir, entry.getName());

        if (entry.isDirectory()) {
            outputFile.mkdirs();
            return;
        }

        if (!outputFile.getParentFile().exists()) {
            outputFile.getParentFile().mkdirs();
        }

        BufferedInputStream inputStream = new BufferedInputStream(
                zipfile.getInputStream(entry));
        BufferedOutputStream outputStream = new BufferedOutputStream(
                new FileOutputStream(outputFile));

        try {
            byte[] buf = new byte[1024 * 1024];
            int k;
            while ((k = inputStream.read(buf)) != -1) {
                outputStream.write(buf, 0, k);
                outputStream.flush();
            }
        } finally {
            try {
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                inputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Related

  1. download(URL url)
  2. download(URL url, File destination)
  3. download(URL url, File file)
  4. downloadAndExtract(String fileURL, String targetDirectory)
  5. downloadAndSaveImage(URL imageUrl, String path)
  6. downloadAsString(String url)
  7. downloadBinary(URL BaseURL, String Name, File TargetDirectory)
  8. downloadData(String url, String params)
  9. downloadDirectory(URL dirUrl, File destDir)