在Java中使用Zip和GZip文件 [英] Working with Zip and GZip files in Java

查看:200
本文介绍了在Java中使用Zip和GZip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了Java I / O,而且我还没有意识到使用Zip和GZip文件的最新正确方法。我不一定需要一个完整的工作演示 - 我主要是在寻找正确的界面和方法来使用。是的,我可以查找任何随机的教程,但性能是一个问题(这些文件可以变得相当大),我很在意使用最好的工具来完成这项工作。



我将要实现的基本流程:


  • 下载一堆文件(可能是压缩文件,gzip压缩文件,或者两者都有)
  • 将所有提取的文件添加到临时文件夹中的新zip文件中。



输入文件可能会被压缩和归档多次。例如,全面提取应采取以下任何输入(我不能控制这些),并留下 foo.txt




  • foo.txt.gz

  • foo.txt.zip

  • foo.txt.gz.zip

  • foo.txt.zip.gz

  • ...

  • foo.txt.gz.gz.gz.zip.gz.zip.zip.gz.gz

  • ...



然后,我可能会留下 foo.txt bar.mp3 baz.exe - 所以我只是将它们全部添加到一个带有一些通用名称的新zip文件。文件大小是一个潜在的问题,哪个(接口/类) /方法)我应该快速使用


  • 提取zip文件

  • 提取gzip文件?
  • 编写zip文件?

  • 我最好保留个别提取的文件在记忆之前写回磁盘?或者,

  • 可能是大文件, / div>

    请注意,下面建议的TrueZip库已被 TrueVFS 所取代。 p>




    我找到了 TrueZIP库有用。它允许您将归档文件视为只是另一个文件系统,并使用熟悉的Java I / O API。

    与java.util.zip API不同, TrueZIP提供对档案内容的随机访问,所以文件大小不应该成为问题。如果我没有记错的话,它会检测归档文件,而不是在将它们放入归档文件时尝试冗余压缩。



    引用TrueZIP页面:


    TrueZIP API提供了众所周知的类File,FileInputStream和FileOutputStream的替代方案。这种设计使得TrueZIP的使用非常简单:只需要​​为大多数客户端应用程序归档就可以为de.schlichtherle.io包添加一些导入语句,并在需要的地方添加一些类型转换。

    现在,您可以简单地使用路径名称中的目录来存档存档文件。例如,路径名archive.zip/readme在ZIP文件archive.zip中寻址存档条目readme。请注意,文件名后缀是完全可配置的,TrueZIP会自动检测到误报,并将其恢复为普通文件或目录。这是递归的工作,所以一个档案文件甚至可以被包含在另一个档案文件中,如outer.zip/inner.zip/readme。


    It's been a while since I've done Java I/O, and I'm not aware of the latest "right" ways to work with Zip and GZip files. I don't necessarily need a full working demo - I'm primarily looking for the right interfaces and methods to be using. Yes, I could look up any random tutorial on this, but performance is an issue (these files can get pretty big) and I do care about using the best tool for the job.

    The basic process I'll be implementing:

    • Download a bunch of files (that might be zipped, gzipped, or both) to a temp folder.
    • Add all the extracted files to a new zip file in a temp folder.

    The input files might be compressed and archived more than once. For example, the "full extraction" should take any of the following inputs (I'm not in control of these), and leave behind foo.txt:

    • foo.txt.gz
    • foo.txt.zip
    • foo.txt.gz.zip
    • foo.txt.zip.gz
    • ...
    • foo.txt.gz.gz.gz.zip.gz.zip.zip.gz.gz
    • ...

    Then, I might be left with foo.txt, bar.mp3, baz.exe - so I would just add them all to a new zip file with some generic name.

    Questions:

    • With file size being a potential concern, which (interfaces/classes/methods) should I use to quickly:
      • extract zip files?
      • extract gzip files?
      • write zip files?
    • Am I better off keeping the individual extracted files in memory before writing back to the disk? Or,
    • Do potentially large files make that a bad idea?

    解决方案

    Note that TrueZip, the library suggested below, has been superseded by TrueVFS.


    I've found the TrueZIP library useful. It allows you to treat archive files as if they're just another file system and use the familiar Java I/O APIs.

    Unlike the java.util.zip API, TrueZIP provides random access to the contents of the archive, so file size should not be a concern. If I remember correctly, it will detect archive files and not try to redundantly compress them when you put them into an archive.

    Quoting the TrueZIP page:

    The TrueZIP API provides drop-in replacements for the well-known classes File , FileInputStream and FileOutputStream . This design makes TrueZIP very simple to use: All that is required to archive-enable most client applications is to add a few import statements for the package de.schlichtherle.io and add some type casts where required.

    Now you can simply address archive files like directories in a path name. For example, the path name "archive.zip/readme" addresses the archive entry "readme" within the ZIP file "archive.zip". Note that file name suffixes are fully configurable and TrueZIP automatically detects false positives and reverts back to treat them like ordinary files or directories. This works recursively, so an archive file may even be enclosed in another archive file, like in "outer.zip/inner.zip/readme".

    这篇关于在Java中使用Zip和GZip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆