Java实现文件的压缩和解压

时间:2013-08-06 11:53:26
【文件属性】:

文件名称:Java实现文件的压缩和解压

文件大小:1.17MB

文件格式:RAR

更新时间:2013-08-06 11:53:26

ZipOutputStream ZipInputStream

基于GUI用IO流中的ZipOutputStream ,ZipInputStream实现文件的解压与压缩, 如文件解压函数如下 private void unZip(String zipFileName, String outputDirectory)throws Exception { InputStream in=null; FileOutputStream out=null; try { ZipFile zipFile = new ZipFile(zipFileName); java.util.Enumeration e = zipFile.getEntries(); ZipEntry zipEntry = null; createDirectory(outputDirectory, ""); while (e.hasMoreElements()) { zipEntry = (ZipEntry) e.nextElement(); System.out.println("正在解压: " + zipEntry.getName()); String name= null; if (zipEntry.isDirectory()) { name = zipEntry.getName(); name = name.substring(0, name.length() - 1); File f = new File(outputDirectory + File.separator + name); f.mkdir(); System.out.println("创建目录:" + outputDirectory + File.separator + name); } else { String fileName = zipEntry.getName(); fileName = fileName.replace('\\', '/'); if (fileName.indexOf("/") != -1) { createDirectory(outputDirectory, fileName.substring(0,fileName.lastIndexOf("/"))); fileName = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length()); } File f = new File(outputDirectory + File.separator + zipEntry.getName()); f.createNewFile(); in = zipFile.getInputStream(zipEntry); out = new FileOutputStream(f); byte[] by = new byte[1024]; int c; while ((c = in.read(by)) != -1) { out.write(by, 0, c); } out.flush(); } } } catch (Exception ex) { System.out.println(ex.getMessage()); }finally{ out.close(); in.close(); } }


【文件预览】:
ZipExercise
----.project(387B)
----.settings()
--------org.eclipse.jdt.core.prefs(330B)
----src()
--------cn()
----.classpath(353B)
----lib()
--------ant.jar(1.26MB)
----classes()
--------cn()

网友评论

  • 基本功能实现了有参考价值
  • 界面不好,不能压缩目录···
  • 不能压缩目录的~~~