How to Compress/Decompress tar.gz files in java

前端 未结 7 1118
粉色の甜心
粉色の甜心 2020-12-02 17:05

Can anyone show me the correct way to compress and decompress tar.gzip files in java i\'ve been searching but the most i can find is either zip or gzip(alone).

7条回答
  •  半阙折子戏
    2020-12-02 17:26

    My favorite is plexus-archiver - see sources on GitHub.

    Another option is Apache commons-compress - (see mvnrepository).

    With plexus-utils, the code for unarchiving looks like this:

    final TarGZipUnArchiver ua = new TarGZipUnArchiver();
    // Logging - as @Akom noted, logging is mandatory in newer versions, so you can use a code like this to configure it:
    ConsoleLoggerManager manager = new ConsoleLoggerManager();
    manager.initialize();
    ua.enableLogging(manager.getLoggerForComponent("bla"));
    // -- end of logging part
    ua.setSourceFile(sourceFile);
    destDir.mkdirs();
    ua.setDestDirectory(destDir);
    ua.extract();
    

    Similar *Archiver classes are there for archiving.

    With Maven, you can use this dependency:

    
      org.codehaus.plexus
      plexus-archiver
      2.2
    
    

提交回复
热议问题