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

ggczys 2015-05-06 05:34:24
[/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的。
...全文
240 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ggczys 2015-05-08
  • 打赏
  • 举报
回复
问题已经解决,感谢各位。

50,549

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧