写的递归解压zip文件的程序。谢谢大家,帮忙看看

ggczys ggczys 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
[/code]package jy;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;

import org.apache.tools.zip.ZipFile;

import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;





public class Jieya {
public static void unZip(String zipFilePath,String descDir) throws IOException{
try {
System.out.println(zipFilePath);
ZipFile zip = new ZipFile(zipFilePath);
ZipEntry entry = null;
for(Enumeration<?> entries = zip.getEntries();entries.hasMoreElements();){
entry = (ZipEntry) entries.nextElement();
InputStream in = zip.getInputStream((org.apache.tools.zip.ZipEntry) entry);
File zFile = new File(descDir+"//"+entry.getName());
File fPath = new File(zFile.getParentFile().getPath());
//如果是文件夹的话,创建
if(entry.isDirectory()){
if(!zFile.exists()){
zFile.mkdirs();
zip.close();
}
}else {
if(!fPath.exists()){
fPath.mkdirs();
}
OutputStream out = new FileOutputStream(zFile);
String zfilePath = zFile.getAbsolutePath();
if(zfilePath.endsWith(".zip")){
unZip(zfilePath,zfilePath.substring(0,zfilePath.lastIndexOf(".zip")));
File file = new File(zfilePath);
file.delete();
}else if(zfilePath.endsWith(".rar")){
//unRar(zfilePath,zfilePath.substring(0,zfilePath.lastIndexOf(".rar")));
System.out.println("调用别的方法!");
}
byte[] buf1 = new byte[1024];
int len;
while((len=in.read(buf1))>0){
out.write(buf1,0,len);
}
in.close();
out.close();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.err.println(e.getMessage());
}
System.out.println(zipFilePath+"\t解压完毕!");
}
//一个目录下所有文件,返回当前文件的绝对路径
public static void readFile(File file,List<String> resultFileName){
File[] files = file.listFiles();
if(files==null) return;
for(int i=0;i<files.length;i++){
if(files[i].isDirectory()){
resultFileName.add(files[i].getName());
readFile(files[i],resultFileName);
}else if(files[i].isFile()){
resultFileName.add(files[i].getName());
}
}
}
public static void main(String args[]) throws Exception{
//List<String> result = new ArrayList<String>();
//File file = new File("E:/test");
//readFile(file,result);
String zipFilePath = "e:/学习ing.zip";
String descDir = "e:/test";
if(zipFilePath.endsWith(".zip")){
unZip(zipFilePath,descDir);
}else if(zipFilePath.endsWith(".rar")){
//unRar(zipFilePath,descDir);
System.out.println("别的方法");
}
}
}


这是报错:
java.io.IOException: Negative seek offset
at java.io.RandomAccessFile.seek(Native Method)
at org.apache.tools.zip.ZipFile.positionAtCentralDirectory(ZipFile.java:347)
at org.apache.tools.zip.ZipFile.populateFromCentralDirectory(ZipFile.java:243)
at org.apache.tools.zip.ZipFile.<init>(ZipFile.java:142)
at org.apache.tools.zip.ZipFile.<init>(ZipFile.java:113)
at jy.Jieya.unZip(Jieya.java:26)
at jy.Jieya.unZip(Jieya.java:46)
at jy.Jieya.main(Jieya.java:89)
Negative seek offset


文件是我直接压缩成zip的。
...全文
评论 1
收藏 已收藏
分享
返回顶部
ggczys
Java 51,407 成员 86,129 内容