本系列校训
用免费公开视频,卷飞培训班哈人!打死不报班,赚钱靠狠干!
只要自己有电脑,前后项目都能搞!N年苦学无人问,一朝成名天下知!
互相伤害互相卷,玩命学习要你管,天生我才必有用,我命由我不由天!
本文AI使用 https://inscode.csdn.net/
缘由
今天碰上了一件事情。
解压一组ZIP文件,按说这种东西,不就是百度?
ZipFile轻松搞定呀。
百度一下还不分分钟?
public static void unzip(String zipFilePath, String destFolderPath) {
try {
// 创建目标文件夹
File destFolder = new File(destFolderPath);
if(!destFolder.exists()) {
destFolder.mkdirs();
}
// 打开ZIP文件
ZipFile zipFile = new ZipFile(zipFilePath);
// 循环读取ZIP文件中的每个文件
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while(entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
String entryName = entry.getName();
File entryFile = new File(destFolderPath + File.separator + entryName);
if(!entryFile.getParentFile().exists()) {
entryFile.getParentFile().mkdirs();
}
if(!entry.isDirectory()) {
InputStream inputStream = zipFile.getInputStream(entry);
FileOutputStream outputStream = new FileOutputStream(entryFile);
byte[] buffer = new byte[1024];
int len;
while((len = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
}
inputStream.close();
outputStream.close();
}
}
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
但是,这么简单的问题异常了。
开始的时候,我以为是目录有中文名的事情。结果发现,不是
跟目录有没有中文无关
做了断点,发现是能打开ZIP文件。然后,一进到第一个文件就报错。
上AI
我正好手上打开了InsCode AI。我也不百度了,直接AI。
AI很有趣,开始的时候,直接给linux 命令行的。
前面的两个辅导的代码都没有问题。
然后问AI这个压缩文件的问题。
我发现,他给出来的,比我百度到的效率要高不少。真心的,如果AI便宜。我就算不用也买一个,真的比百度挨个网页看要方便太多了。百度上“搬”将太多了,大量的无效代码。JDK1.4 ,JDK1.3的都还能搜得到。反正“搬”的人又不管程序是不是真的能用。
让AI分析错误
然后,我就把报的错给他了。让他不使用ZIPFile 。
对于出现了 MALFORMED 异常,问题通常是由于 ZIP 文件格式损坏或编码问题导致的。您可以尝试使用另一个解压缩软件打开 ZIP 文件来确认是否是 ZIP 文件本身的问题。如果不是 ZIP 文件本身的问题,建议您检查 ZIP 文件中是否包含非法字符或特殊字符。
他还给出了JDK版本。
真的是贴心
此代码使用 Java 8 中的 ZipFile 类来打开 ZIP 文件,并使用 stream() 方法遍历 ZIP 文件中所有的 ZipEntry,然后读取 ZIP 文件数据并写入解压缩输出流。
另外,如果您仍然无法解决问题,建议您尝试使用其他第三方类库,如 Apache Commons Compress。这些类库通常可以更好地处理不同的 ZIP 文件格式和编码问题。
如果您的 ZIP 文件确实存在问题,可以尝试使用 ZipFile 类来打开 ZIP 文件,并使用 ZipEntry 类来读取 ZIP 文件中的内容。以下是修改过的代码:
天啦,AI竟然懂了我的意思。基于ZipInputStream的
又给出了代码:
public static void unzipio(String zipFilePath, String destFolderPath) {
try {
// 创建目标文件夹
// 创建目标文件夹
File destFolder = new File(destFolderPath);
if(!destFolder.exists()) {
destFolder.mkdirs();
}
// 打开ZIP文件输入流
FileInputStream fileInputStream = new FileInputStream(zipFilePath);
ZipInputStream zipInputStream = new ZipInputStream(fileInputStream);
// 循环读取ZIP文件中的每个文件
ZipEntry entry = null;
while((entry = zipInputStream.getNextEntry()) != null) {
String entryName = entry.getName();
File entryFile = new File(destFolderPath + File.separator + entryName);
if(!entryFile.getParentFile().exists()) {
entryFile.getParentFile().mkdirs();
}
if(!entry.isDirectory()) {
FileOutputStream outputStream = new FileOutputStream(entryFile);
byte[] buffer = new byte[1024];
int len;
while((len = zipInputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
}
outputStream.close();
}
zipInputStream.closeEntry();
}
zipInputStream.close();
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
但是,结果是杯具的。
人工指导纠正方向
然后,我就知道了这应该就是ZIP格式的问题,于是我再问AI。
他用了一个我不常见的包。因为我只记得有一个包是junrar
然后,我再限定AI。
这个示例代码中,我们使用了 junrar 库中的 Archive 类来创建解压缩输入流对象,并使用 nextFileHeader() 方法遍历 RAR 文件中的所有文件条目,然后读取文件数据并写入解压缩输出流。
在使用 junrar 库时,需要将其添加到项目依赖库中,例如通过 Maven 添加以下依赖:
<dependency>
<groupId>com.github.junrar</groupId>
<artifactId>junrar</artifactId>
<version>7.4.0</version>
</dependency>
或者下载 junrar 的 Jar 包并将其添加到项目中。
全部的代码如下:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import de.innosystec.unrar.Archive;
import de.innosystec.unrar.rarfile.FileHeader;
public class UnrarFile {
public static void main(String[] args) throws IOException {
// 定义RAR文件路径和解压缩目标路径
String rarPath = "C:\\example\\example.rar";
String targetPath = "C:\\example\\unzipped\\";
// 创建解压缩输入流对象
Archive archive = new Archive(new File(rarPath));
try {
FileHeader fileHeader = archive.nextFileHeader();
while (fileHeader != null) {
String entryName = fileHeader.getFileNameString().trim();
if (!fileHeader.isDirectory()) {
System.out.println("Extracting: " + entryName);
// 创建解压缩输出流对象
File targetFile = new File(targetPath + entryName);
OutputStream outputStream = new FileOutputStream(targetFile);
// 读取RAR文件数据并写入解压缩输出流
archive.extractFile(fileHeader, outputStream);
outputStream.close();
}
fileHeader = archive.nextFileHeader();
}
} finally {
archive.close();
}
System.out.println("Done!");
}
}
然后,这个问题解决了
天啦,现在就是简单的AI,也可以把初级的程序员秒杀了。
彻底解决ZIP包的问题
即然知道是什么坑了,那就简单了,上zip4j,同样的,百度找代码真的不习惯了,直接问AI。
因为这次代码少,
所以放一个全图。
这个示例代码中,我们使用了 zip4j 库中的 ZipFile 类来创建 ZIP 文件对象,并使用 extractAll() 方法将 ZIP 文件解压缩到指定目录中。
在使用 zip4j 库时,需要将其添加到项目依赖库中,例如通过 Maven 添加以下依赖:
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>2.7.0</version>
</dependency>
或者下载 zip4j 的 Jar 包并将其添加到项目中。
这就是AI给的结果,
代码如下。
import java.io.File;
import java.io.IOException;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
public class UnzipFile {
public static void main(String[] args) throws IOException, ZipException {
// 定义ZIP文件路径和解压缩目标路径
String zipPath = "C:\\example\\example.zip";
String targetPath = "C:\\example\\unzipped\\";
// 创建 ZipFile 对象并设置 ZIP 文件密码(如果有)
ZipFile zipFile = new ZipFile(zipPath);
String password = "password123";
if (zipFile.isEncrypted()) {
zipFile.setPassword(password);
}
// 解压 ZIP 文件
zipFile.extractAll(targetPath);
System.out.println("Done!");
}
}
Done!
就问你,现在怕不怕?
总结:这是什么样的坑?
这不是一个JAVA的坑,这也不是一个JAVA算法的坑,更加的不是ZipFile的坑,而且AI出来了之后,给全体的初级程序员挖了一个天大的坑。
如果程序员再没有自己的想法,只会百度“搬” 那不需要你了。Ai搬的比你还好,“快”,“有效”,“无错”,而且,他还会分析出错误,“给出更好的答案”。
建议
本文AI使用 https://inscode.csdn.net/
后面的Ai是不是“搬” 我也有点弄不清了。但是也不重要了,只知道一点就是,AI远比初级程序员强就行了。AI缺少的是把一些小片段组成项目需求。那是不是意味着,以后IT的编程,是需求分析了之后,需求分析师对着AI一通输出?直接秒杀一众的程序员?算法还是AI?面向AI编程将很快来临!让暴风雨来的更猛烈些吧!