打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Java中zip的压缩和解压缩

在Java中可以使用ZipOutputStream和ZipInputStream来实现zip的压缩和解压缩操作,另外使用FileSystem也可以用来实现zip的解压缩,下面将介绍这几种方式,直接上代码。

zip压缩

待压缩文件目录结构:

每个zip文件项都要对应一个ZipEntry,然后通过ZipOutputStream的putNextEntry方法开始写入一个新的zip文件项,将文件数据发送到zip输出流中,完成后再调用closeEntry方法。

@Testpublic void testCompressByZip() {try (//指定压缩完成后zip文件的存储路径ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream("F:\\myFavorites.zip"))){//待压缩文件/目录所在的目录File fileFolder = new File("F:\\我的收藏");//获取目录下的所有文件File[] files = fileFolder.listFiles();ZipEntry zipEntry;byte[] byteArray;int len;//遍历目录下的所有文件/目录,并将它们添加到压缩文件中for (File file : files) {//一个ZipEntry对应压缩文件中的一项zipEntry = new ZipEntry(file.getName());zipOutputStream.putNextEntry(zipEntry);try (FileInputStream in = new FileInputStream(file)) {byteArray = new byte[1024];while ((len = in.read(byteArray)) != -1) {zipOutputStream.write(byteArray, 0, len);}} catch (IOException ex) {ex.printStackTrace();}zipOutputStream.closeEntry();}} catch (IOException ex) {ex.printStackTrace();}}

压缩结果:

zip解压缩

遍历zip文件中的所有项,并获取对应项的输入流,然后通过FileOutputStream输出到指定目录中。

@Testpublic void testDecompressByZip() {try (//指定需要解压缩的zip文件ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream("F:\\myFavorites.zip"))){ZipEntry zipEntry;byte[] byteArray;int len;//遍历zip文件中的所有项,并逐个解压到指定的目录中while ((zipEntry = zipInputStream.getNextEntry()) != null) {try (FileOutputStream fileOutputStream = new FileOutputStream("F:\\我的解压目录\\" + zipEntry.getName())) {byteArray = new byte[1024];while ((len = zipInputStream.read(byteArray)) != -1) {fileOutputStream.write(byteArray, 0, len);}} catch (IOException ex) {ex.printStackTrace();}}} catch (IOException ex) {ex.printStackTrace();}}

解压缩结果:

使用FileSystem解压缩

  1. 建立一个文件系统,包含zip文件中的所有项。
  2. 遍历zip文件中的所有项,通过文件访问器SimpleFileVisitor将每个项复制到指定目录中。
@Testpublic void testDecompressByZip2(){try {//将压缩文件作为一个文件系统访问FileSystem fileSystem = FileSystems.newFileSystem(Paths.get("F:\\myFavorites.zip"), null);//遍历压缩文件中的内容,并使用文件访问器访问所有内容Files.walkFileTree(fileSystem.getPath("/"), new SimpleFileVisitor<Path>() {@Overridepublic FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {System.out.println("访问目录中文件时调用");//将文件解压到指定目录Files.copy(file, new FileOutputStream("F:\\我的解压目录\\" + file.getName(file.getNameCount() - 1)));return FileVisitResult.CONTINUE;}@Overridepublic FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {System.out.println("访问目录中文件之前调用");return FileVisitResult.CONTINUE;}@Overridepublic FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {System.out.println("访问目录中文件失败时调用");return FileVisitResult.SKIP_SUBTREE;}@Overridepublic FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {System.out.println("访问目录中的文件及其所有后代之后调用");return FileVisitResult.CONTINUE;}});} catch (IOException ex) {ex.printStackTrace();}}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
java压缩和解压缩Zip、Jar、Gzip文件
zip
何应用java.util.zip包现数据压缩与解压
利用JAVA API函数实现数据的压缩与解压缩(3)
如何将文件打成zip包并下载
使用Java?API压缩和?解压缩数据
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服