Module bus.extra

Class StreamArchiver

java.lang.Object
org.miaixz.bus.extra.compress.archiver.StreamArchiver
All Implemented Interfaces:
Closeable, AutoCloseable, Archiver

public class StreamArchiver extends Object implements Archiver
Data archiver wrapper, which archives several files or directories into a compressed package. Supported archive file formats are:
  • ArchiveStreamFactory.AR
  • ArchiveStreamFactory.CPIO
  • ArchiveStreamFactory.JAR
  • ArchiveStreamFactory.TAR
  • ArchiveStreamFactory.ZIP
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • StreamArchiver

      public StreamArchiver(Charset charset, String archiverName, File file)
      Constructor.
      Parameters:
      charset - The character encoding.
      archiverName - The name of the archive format, see ArchiveStreamFactory.
      file - The output archive file.
    • StreamArchiver

      public StreamArchiver(Charset charset, String archiverName, OutputStream targetStream)
      Constructor.
      Parameters:
      charset - The character encoding.
      archiverName - The name of the archive format, see ArchiveStreamFactory.
      targetStream - The output stream for the archive.
  • Method Details

    • of

      public static StreamArchiver of(Charset charset, String archiverName, File file)
      Creates an archiver.
      Parameters:
      charset - The character encoding.
      archiverName - The name of the archive format, see ArchiveStreamFactory.
      file - The output archive file.
      Returns:
      A new StreamArchiver instance.
    • of

      public static StreamArchiver of(Charset charset, String archiverName, OutputStream out)
      Creates an archiver.
      Parameters:
      charset - The character encoding.
      archiverName - The name of the archive format, see ArchiveStreamFactory.
      out - The output stream for the archive.
      Returns:
      A new StreamArchiver instance.
    • add

      public StreamArchiver add(File file, String path, Function<String,String> fileNameEditor, Predicate<File> predicate) throws org.miaixz.bus.core.lang.exception.InternalException
      Description copied from interface: Archiver
      Adds a file or directory to the archive package. Directories are added recursively level by level.
      Specified by:
      add in interface Archiver
      Parameters:
      file - The file or directory.
      path - The initial path of the file or directory. If null, it is placed at the root level.
      fileNameEditor - A function to edit the file name.
      predicate - A file filter that specifies which files or directories can be added. If Predicate.test(Object) is true, the file is kept. If null, all are added.
      Returns:
      this
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException
    • finish

      public StreamArchiver finish()
      Finishes archiving the added files. This method does not close the archive stream, allowing more files to be added.
      Specified by:
      finish in interface Archiver
      Returns:
      this
    • close

      public void close()
      Description copied from interface: Archiver
      Closes without throwing an exception.
      Specified by:
      close in interface Archiver
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable