gpt4 book ai didi

java.io.filenotfoundException 下载 zip 并解压

转载 作者:太空宇宙 更新时间:2023-11-04 14:03:22 24 4
gpt4 key购买 nike

我制作了一个小程序,从直接链接下载 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();
// 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();
}
}

这是错误:

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)

最佳答案

显然这个文件还没有创建。您可以使用以下命令创建它

new File(yourFilepath).createNewFile()

并且您需要调用之前执行此操作

FileOutputStream fos = new FileOutputStream(filepath + filename);

抛出异常。然后就可以正常工作了。

关于java.io.filenotfoundException 下载 zip 并解压,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29103251/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com