用JAVA实现“文件和文件夹的解压”

yhxepoh 2008-06-30 10:41:48
我在本论坛上看到许多关于这个的解释和代码,但是代码都不能实现,因为是我太笨了吧,虽然作JAVA有段时间,但还是没有入门阿,我希望大家能给我个简单,明了的例子,像我的压缩代码一样:
import java.io.*;
import java.util.zip.*;

public class SimpleZip {

private static ZipOutputStream zos;

public static void main(String[] args) {

String fileName = "d:/try";
try {
makeZip(fileName);
}
catch (Exception e) {
System.err.println(e);
}
}

public static void makeZip(String fileName) throws IOException,
FileNotFoundException {
File file = new File(fileName);
zos = new ZipOutputStream(new FileOutputStream(file + ".zip"));
recurseFiles(file);
zos.close();
}

private static void recurseFiles(File file) throws IOException,
FileNotFoundException {
if (file.isDirectory()) {
String[] fileNames = file.list();
if (fileNames != null) {
for (int i = 0; i < fileNames.length; i++) {
recurseFiles(new File(file, fileNames[i]));
}
}
}
else {
byte[] buf = new byte[1024];
int len;

ZipEntry zipEntry = new ZipEntry(file.toString());
FileInputStream fin = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fin);
zos.putNextEntry(zipEntry);

while ((len = in.read(buf)) >= 0) {
zos.write(buf, 0, len);
}

in.close();

zos.closeEntry();
}
}
}


附带:我是新手没有分给大家,希望大家谅解!!!!
...全文
80 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuzi123 2008-07-04
  • 打赏
  • 举报
回复
顶个
yhxepoh 2008-06-30
  • 打赏
  • 举报
回复
哦,谢谢
tsizhao 2008-06-30
  • 打赏
  • 举报
回复
org.apache.tools.zip.ZipFile 在ant.jar包里。getRealFileName方法只是实现 给定根目录,返回一个相对路径所对应的实际文件名.这个不是主要的,你可以自己实现他,主要的已经给你了!~~~~
yhxepoh 2008-06-30
  • 打赏
  • 举报
回复
大哥能不能给我个完整的啊,你代码中的“zfile.getEntries”和“getRealFileName”方法我导进去出现了错误!!!
tsizhao 2008-06-30
  • 打赏
  • 举报
回复
/**
* 测试解压缩功能. 将d:\\download\\test.zip文件解压到d:\\temp\\zipout目录下.
* @param baseDir 目录
* @param file 要解压的ZIP文件
* @throws Exception
*/
public static void ExtractZip(String baseDir,String file) throws Exception {
// InputStream is=new BufferedInputStream(new FileInputStream());
//String baseDir = "D:\\workspace\\swing(js)\\";
ZipFile zfile = new ZipFile(baseDir + File.separator + file);
//System.out.println(zfile.getEntries());
Enumeration zList = zfile.getEntries();
ZipEntry ze = null;
byte[] buf = new byte[1024];
while (zList.hasMoreElements()) {
// 从ZipFile中得到一个ZipEntry
ze = (ZipEntry) zList.nextElement();
if (ze.isDirectory()) {
System.out.println("Dir: " + ze.getName() + " skipped..");
continue;
}
if(ze.getSize()<=0)
throw new Exception("文件长度为0");
System.out.println("Extracting: " + ze.getName() + "\t"
+ ze.getSize() + "\t" + ze.getCompressedSize());

// 以ZipEntry为参数得到一个InputStream,并写到OutputStream中
OutputStream os = new BufferedOutputStream(new FileOutputStream(
getRealFileName(baseDir, ze.getName())));
InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
int readLen = 0;
while ((readLen = is.read(buf, 0, 1024)) != -1) {
os.write(buf, 0, readLen);
}
is.close();
os.close();
System.out.println("Extracted: " + ze.getName());
}
zfile.close();
}

67,538

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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