java的一个ZIP解压问题求解。

rayhuang7 2012-08-01 03:43:21
我现在有个文件加,里面放的都是zip文件,我现在想要的不是要每次输入文件名的地址,而是让程序自动找到zip文件然后解压,然后放到指定的文件加里。我现在写的程序是只能一个一个的输入文件名然后他才能执行解压缩。

我的代码如下。
public static void UnZIP(String sourceFileName, String desDir) throws ZipException, IOException {

// erstellen ein Object von ZIP
ZipFile zf = new ZipFile(new File(sourceFileName));

// alle Entries bekommen
Enumeration<ZipEntry> en = (Enumeration<ZipEntry>) zf.entries();
int length = 0;
byte[] b = new byte[2048];

while (en.hasMoreElements()) {
ZipEntry ze = en.nextElement();

File f = new File(desDir + ze.getName());

if (ze.isDirectory()) {
f.mkdirs();
} else {

if (!f.getParentFile().exists()) {
f.getParentFile().mkdirs();
}


OutputStream outputStream = new FileOutputStream(f);
java.io.InputStream inputStream = zf.getInputStream(ze);
while ((length = inputStream.read(b)) > 0)
outputStream.write(b, 0, length);

inputStream.close();
outputStream.close();
}
}
zf.close();

}

那位大虾能帮忙改改。
...全文
110 7 打赏 收藏 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
rayhuang7 2012-08-01
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

直接整个放到方法体中就行。
[/Quote]

我加了并且改戏了程序还是不行。
rayhuang7 2012-08-01
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

直接整个放到方法体中就行。
[/Quote]
是这样吗??
public static void UnZIP(String sourceFileName, String desDir) throws ZipException, IOException {

// erstellen ein Object von ZIP
ZipFile zf = new ZipFile(new File(sourceFileName));

// alle Entries bekommen
Enumeration<ZipEntry> en = (Enumeration<ZipEntry>) zf.entries();
int length = 0;
byte[] b = new byte[2048];

while (en.hasMoreElements()) {
ZipEntry ze = en.nextElement();

File f = new File(desDir + ze.getName());

File [] files = new File(dirPath).listFiles(new FileFilter(){

@Override
public boolean accept(File aFile)
{
return aFile.getName().toLowerCase().endsWith(".zip");
}
});

if (ze.isDirectory()) {
f.mkdirs();
} else {

if (!f.getParentFile().exists()) {
f.getParentFile().mkdirs();
}


OutputStream outputStream = new FileOutputStream(f);
java.io.InputStream inputStream = zf.getInputStream(ze);
while ((length = inputStream.read(b)) > 0)
outputStream.write(b, 0, length);

inputStream.close();
outputStream.close();
}
}
zf.close();

}

这样??
xu200074212 2012-08-01
  • 打赏
  • 举报
回复
直接整个放到方法体中就行。
rayhuang7 2012-08-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

File [] files = new File(dirPath).listFiles(new FileFilter(){

@Override
public boolean accept(File aFile)
{
return aFile.getName().to……
[/Quote]

这个代码怎么放到我的代码中呢??能否有个完整的。
sffx123 2012-08-01
  • 打赏
  • 举报
回复
循环目录得到zip文件然后解压即可
菖蒲老先生 2012-08-01
  • 打赏
  • 举报
回复
重写个文件格式过滤的,楼上的就可以。
xu200074212 2012-08-01
  • 打赏
  • 举报
回复
File [] files = new File(dirPath).listFiles(new FileFilter(){

@Override
public boolean accept(File aFile)
{
return aFile.getName().toLowerCase().endsWith(".zip");
}
});

再对files进行for循环,不能解决你的问题吗?

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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