集册 Java实例教程 从URL下载文件并解压缩

从URL下载文件并解压缩

欢马劈雪     最近更新时间:2020-01-02 10:19:05

658
从URL下载文件并解压缩
/*from NowJava.com - 时代Java*/


import java.io.*;

import java.net.URL;

import java.nio.file.Files;

import java.nio.file.Paths;

import java.nio.file.StandardCopyOption;

import java.util.zip.ZipEntry;

import java.util.zip.ZipInputStream;


public class Main{


    public static void downloadFileInto(CharSequence stringURL,

            File directory) {

        try {

            URL url = new URL(stringURL.toString());

            unzipIntoDirectory(url.openStream(), directory);

        } catch (IOException e) {

            e.printStackTrace();

        }

    }/**时   代     Java  公  众  号 - nowjava.com**/

    public static void unzipIntoDirectory(File file, File directory) {

        try {

            unzipIntoDirectory(new FileInputStream(file), directory);

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        }

    }

    public static void unzipIntoDirectory(InputStream inputStream,

            File directory) {

        if (directory.isFile())

            return;

        directory.mkdirs();


        try {

            inputStream = new BufferedInputStream(inputStream);

            inputStream = new ZipInputStream(inputStream);


            for (ZipEntry entry = null; (entry = ((ZipInputStream) inputStream)

                    .getNextEntry()) != null;) {

                StringBuilder pathBuilder = new StringBuilder(

                        directory.getPath()).append('/').append(

                        entry.getName());

                File file = new File(pathBuilder.toString());


                
展开阅读全文