java.io.filenotfoundexception下载zip并提取它 [英] java.io.filenotfoundexception downloading zip and extracting it

查看:218
本文介绍了java.io.filenotfoundexception下载zip并提取它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个小程序,从直接链接下载zip文件,然后将其中的所有内容提取到同一目录中。
它不下载任何东西,也没有提取。
这是我迄今为止所做的:

  package main; 

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {

static String url =https://www.dropbox.com/s/uml938guklfvo7r/Tekst.zip?dl=1;
static int lastSlashIndex = url.lastIndexOf('/');
static String filename = url.substring(lastSlashIndex + 1,url.length() - 5);
static String filepath =C:;
private static final int BUFFER = 4096;

public static void main(String [] args){

try {
URL website = new URL(url);
ReadableByteChannel rbc;
rbc = Channels.newChannel(website.openStream());
new File(filepath + filename).createNewFile();
FileOutputStream fos = new FileOutputStream(filepath + filename);
fos.getChannel()。transferFrom(rbc,0,Long.MAX_VALUE);
fos.close();
} catch(Exception e){e.printStackTrace(); }

try {
unzip(filepath + filename,filepath);
} catch(IOException e){
e.printStackTrace();
}




public static void unzip(String zipFilePath,String destDirectory)throws IOException {
File destDir = new File( destDirectory);
if(!destDir.exists()){
destDir.mkdir();
}
ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry entry = zipIn.getNextEntry();
//遍历zip文件
中的条目(entry!= null){
String filePath = destDirectory + File.separator + entry.getName();
if(!entry.isDirectory()){
//如果条目是一个文件,提取它
extractFile(zipIn,filePath);
} else {
//如果条目是一个目录,使目录
File dir = new File(filePath);
dir.mkdir();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
}

private static void extractFile(ZipInputStream zipIn,String filePath)throws IOException {
Line 68>> BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
byte [] bytesIn = new byte [BUFFER];
int read = 0;
while((read = zipIn.read(bytesIn))!= -1){
bos.write(bytesIn,0,read);
}
bos.close();
}
}

这是错误:

  java.io.FileNotFoundException:C:\Tekst.txt(访问被拒绝)
在java.io.FileOutputStream.open(Native方法)
在java.io.FileOutputStream。< init>(未知源)
在java.io.FileOutputStream。< init>(未知源)
在main.Main.extractFile (Main.java:68)
在main.Main.unzip(Main.java:55)
在main.Main.main(Main.java:35)


解决方案

显然这个文件根本没有创建。您可以使用

 创建新文件(yourFilepath).createNewFile()
/ pre>

,您需要在之前

  FileOutputStream fos = new FileOutputStream(filepath + filename); 

其中引发异常。然后它工作得很好。


I made a small program to download a zip file from a direct link and after that extract all contents of it in the same directory. It doesn't download anything and it also doesn't extract. This is what I have so far:

package main;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {

    static String url = "https://www.dropbox.com/s/uml938guklfvo7r/Tekst.zip?dl=1";
    static int lastSlashIndex = url.lastIndexOf('/');
    static String filename= url.substring(lastSlashIndex + 1, url.length() - 5);
    static String filepath = "C:";
    private static final int BUFFER = 4096;

    public static void main(String[] args) {

        try{
            URL website = new URL(url);
            ReadableByteChannel rbc;
            rbc = Channels.newChannel(website.openStream());
            new File(filepath + filename).createNewFile();
            FileOutputStream fos = new FileOutputStream(filepath + filename);
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            fos.close();
        }catch(Exception e){ e.printStackTrace(); }

        try {
            unzip(filepath + filename, filepath);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public static void unzip(String zipFilePath, String destDirectory) throws IOException {
        File destDir = new File(destDirectory);
        if (!destDir.exists()) {
            destDir.mkdir();
        }
        ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
        ZipEntry entry = zipIn.getNextEntry();
        // iterates over entries in the zip file
        while (entry != null) {
            String filePath = destDirectory + File.separator + entry.getName();
            if (!entry.isDirectory()) {
                // if the entry is a file, extracts it
                extractFile(zipIn, filePath);
            } else {
                // if the entry is a directory, make the directory
                File dir = new File(filePath);
                dir.mkdir();
            }
            zipIn.closeEntry();
            entry = zipIn.getNextEntry();
        }
        zipIn.close();
    }

    private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
        Line 68>> BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] bytesIn = new byte[BUFFER];
        int read = 0;
        while ((read = zipIn.read(bytesIn)) != -1) {
            bos.write(bytesIn, 0, read);
        }
        bos.close();
    }  
}

Here is the error:

java.io.FileNotFoundException: C:\Tekst.txt (Access is denied)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at main.Main.extractFile(Main.java:68)
    at main.Main.unzip(Main.java:55)
    at main.Main.main(Main.java:35)

解决方案

Apparently this file is simply not created yet. You can create it with

new File(yourFilepath).createNewFile()

and you need to do it before you invoke

FileOutputStream fos = new FileOutputStream(filepath + filename);

which throws the exception. Then it works perfectly fine.

这篇关于java.io.filenotfoundexception下载zip并提取它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆