Class XarArchive

    • Constructor Detail

      • XarArchive

        public XarArchive()

        Initializes a new instance of the XarArchive class.

        The following example shows how to compress a file.

        
             try (XarArchive archive = new XarArchive()) {
                 archive.createEntry("first.bin", "data.bin");
                 archive.save("archive.xar");
             }
         
      • XarArchive

        public XarArchive​(XarCompressionSettings defaultCompressionSettings)

        Initializes a new instance of the XarArchive class.

        The following example shows how to compress a file.

        
             try (XarArchive archive = new XarArchive()) {
                 archive.createEntry("first.bin", "data.bin");
                 archive.save("archive.xar");
             }
         
        Parameters:
        defaultCompressionSettings - the default compression settings, applyed to all entries of the archive
      • XarArchive

        public XarArchive​(InputStream sourceStream)

        Initializes a new instance of the XarArchive class and composes an entry list can be extracted from the archive.

        The following example shows how to extract all the entries to a directory.

        
             try (XarArchive archive = new XarArchive(new FileInputStream("archive.xar"))) {
                 archive.extractToDirectory("C:\\extracted");
             } catch (IOException ex) {
             }
         

        This constructor does not unpack any entry. See XarFileEntry.open() method for unpacking.

        Parameters:
        sourceStream - the source of the archive
        Throws:
        com.aspose.ms.System.ArgumentNullException - sourceStream is null.
        com.aspose.ms.System.IO.InvalidDataException - sourceStream is not valid xar archive.
      • XarArchive

        public XarArchive​(InputStream sourceStream,
                          XarLoadOptions loadOptions)

        Initializes a new instance of the XarArchive class and composes an entry list can be extracted from the archive.

        The following example shows how to extract all the entries to a directory.

        
             try (XarArchive archive = new XarArchive(new FileInputStream("archive.xar"))) {
                 archive.extractToDirectory("C:\\extracted");
             } catch (IOException ex) {
             }
         

        This constructor does not unpack any entry. See XarFileEntry.open() method for unpacking.

        Parameters:
        sourceStream - the source of the archive
        loadOptions - the options to load archive with
        Throws:
        com.aspose.ms.System.ArgumentNullException - sourceStream is null.
        com.aspose.ms.System.IO.InvalidDataException - sourceStream is not valid xar archive.
      • XarArchive

        public XarArchive​(String path)

        Initializes a new instance of the XarArchive class and composes an entry list can be extracted from the archive.

        The following example shows how to extract all the entries to a directory.

        
             try (XarArchive archive = new XarArchive("archive.xar")) {
                 archive.extractToDirectory("C:\\extracted");
             }
         

        This constructor does not unpack any entry. See XarFileEntry.open() method for unpacking.

        Parameters:
        path - the path to the archive file
        Throws:
        com.aspose.ms.System.ArgumentNullException - path is null.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access.
        com.aspose.ms.System.ArgumentException - The path is empty, contains only white spaces, or contains invalid characters.
        com.aspose.ms.System.IO.PathTooLongException - The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
        com.aspose.ms.System.NotSupportedException - File at path contains a colon (:) in the middle of the string.
        com.aspose.ms.System.IO.FileNotFoundException - The file is not found.
        com.aspose.ms.System.IO.DirectoryNotFoundException - The specified path is invalid, such as being on an unmapped drive.
        com.aspose.ms.System.IO.IOException - The file is already open.
        com.aspose.ms.System.IO.InvalidDataException - File at path is not valid xar archive.
      • XarArchive

        public XarArchive​(String path,
                          XarLoadOptions loadOptions)

        Initializes a new instance of the XarArchive class and composes an entry list can be extracted from the archive.

        The following example shows how to extract all the entries to a directory.

        
             try (XarArchive archive = new XarArchive("archive.xar")) {
                 archive.extractToDirectory("C:\\extracted");
             }
         

        This constructor does not unpack any entry. See XarFileEntry.open() method for unpacking.

        Parameters:
        path - the path to the archive file
        loadOptions - the options to load archive with
        Throws:
        com.aspose.ms.System.ArgumentNullException - path is null.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access.
        com.aspose.ms.System.ArgumentException - The path is empty, contains only white spaces, or contains invalid characters.
        com.aspose.ms.System.IO.PathTooLongException - The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
        com.aspose.ms.System.NotSupportedException - File at path contains a colon (:) in the middle of the string.
        com.aspose.ms.System.IO.FileNotFoundException - The file is not found.
        com.aspose.ms.System.IO.DirectoryNotFoundException - The specified path is invalid, such as being on an unmapped drive.
        com.aspose.ms.System.IO.IOException - The file is already open.
        com.aspose.ms.System.IO.InvalidDataException - File at path is not valid xar archive.
    • Method Detail

      • getEntries

        public final List<XarEntry> getEntries()

        Gets entries of XarEntry type constituting the archive.

        Returns:
        entries of XarEntry type constituting the archive
      • getFormat

        public final ArchiveFormat getFormat()

        Gets the archive format.

        Specified by:
        getFormat in interface IArchive
        Returns:
        the archive format
      • extractToDirectory

        public final void extractToDirectory​(String destinationDirectory)

        Extracts all the files in the archive to the directory provided.

        
             try (XarArchive archive = new XarArchive("archive.xar")) {
                 archive.extractToDirectory("C:\\extracted");
             }
         
        Specified by:
        extractToDirectory in interface IArchive
        Parameters:
        destinationDirectory - the path to the directory to place the extracted files in.

        If the directory does not exist, it will be created

        Throws:
        com.aspose.ms.System.ArgumentNullException - path is null
        com.aspose.ms.System.IO.PathTooLongException - The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access the existing directory.
        com.aspose.ms.System.NotSupportedException - If the directory does not exist, the path contains a colon character (:) that is not part of a drive label ("C:\").
        com.aspose.ms.System.ArgumentException - path is a zero-length string, contains only white space, or contains one or more invalid characters.
        com.aspose.ms.System.IO.IOException - The directory specified by path is a file. -or- The network name is not known.
        com.aspose.ms.System.IO.InvalidDataException - The archive is corrupted.
      • createEntries

        public final XarArchive createEntries​(String sourceDirectory)

        Adds to the archive all the files and directories recursively in the directory given.

        
             try (FileOutputStream xarFile = new FileOutputStream("archive.xar")) {
                 try (XarArchive archive = new XarArchive()) {
                     archive.createEntries("C:\\folder", false);
                     archive.save(xarFile);
                 }
             } catch (IOException ex) {
             }
         
        Parameters:
        sourceDirectory - directory to compress
        Returns:
        Xar entry instance
        Throws:
        com.aspose.ms.System.ArgumentNullException - sourceDirectory is null.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access sourceDirectory.
        com.aspose.ms.System.ArgumentException - sourceDirectory contains invalid characters such as ", <, >, or |.
        com.aspose.ms.System.IO.PathTooLongException - The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters. The specified path, file name, or both are too long.
        com.aspose.ms.System.IO.IOException - sourceDirectory stands for a file, not for a directory.
      • createEntries

        public final XarArchive createEntries​(String sourceDirectory,
                                              boolean includeRootDirectory)

        Adds to the archive all the files and directories recursively in the directory given.

        
             try (FileOutputStream xarFile = new FileOutputStream("archive.xar")) {
                 try (XarArchive archive = new XarArchive()) {
                     archive.createEntries("C:\\folder", false);
                     archive.save(xarFile);
                 }
             } catch (IOException ex) {
             }
         
        Parameters:
        sourceDirectory - directory to compress
        includeRootDirectory - indicates whether to include the root directory itself or not
        Returns:
        Xar entry instance
        Throws:
        com.aspose.ms.System.ArgumentNullException - sourceDirectory is null.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access sourceDirectory.
        com.aspose.ms.System.ArgumentException - sourceDirectory contains invalid characters such as ", <, >, or |.
        com.aspose.ms.System.IO.PathTooLongException - The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters. The specified path, file name, or both are too long.
        com.aspose.ms.System.IO.IOException - sourceDirectory stands for a file, not for a directory.
      • createEntries

        public final XarArchive createEntries​(String sourceDirectory,
                                              boolean includeRootDirectory,
                                              XarCompressionSettings compressionSettings)

        Adds to the archive all the files and directories recursively in the directory given.

        
             try (FileOutputStream xarFile = new FileOutputStream("archive.xar")) {
                 try (XarArchive archive = new XarArchive()) {
                     archive.createEntries("C:\\folder", false);
                     archive.save(xarFile);
                 }
             } catch (IOException ex) {
             }
         
        Parameters:
        sourceDirectory - directory to compress
        includeRootDirectory - indicates whether to include the root directory itself or not
        compressionSettings - the compression settings used for added XarEntry items
        Returns:
        Xar entry instance
        Throws:
        com.aspose.ms.System.ArgumentNullException - sourceDirectory is null.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access sourceDirectory.
        com.aspose.ms.System.ArgumentException - sourceDirectory contains invalid characters such as ", <, >, or |.
        com.aspose.ms.System.IO.PathTooLongException - The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters. The specified path, file name, or both are too long.
        com.aspose.ms.System.IO.IOException - sourceDirectory stands for a file, not for a directory.
      • createEntries

        public final XarArchive createEntries​(File directory)

        Adds to the archive all the files and directories recursively in the directory given.

        
             try (FileOutputStream xarFile = new FileOutputStream("archive.xar")) {
                 try (XarArchive archive = new XarArchive()) {
                     archive.createEntries(new java.io.File("C:\\folder"), false);
                     archive.save(xarFile);
                 }
             } catch (IOException ex) {
             }
         
        Parameters:
        directory - directory to compress
        Returns:
        Xar entry instance
        Throws:
        com.aspose.ms.System.ArgumentNullException - directory is null.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access directory.
        com.aspose.ms.System.IO.IOException - directory stands for a file, not for a directory.
      • createEntries

        public final XarArchive createEntries​(File directory,
                                              boolean includeRootDirectory)

        Adds to the archive all the files and directories recursively in the directory given.

        
             try (FileOutputStream xarFile = new FileOutputStream("archive.xar")) {
                 try (XarArchive archive = new XarArchive()) {
                     archive.createEntries(new java.io.File("C:\\folder"), false);
                     archive.save(xarFile);
                 }
             } catch (IOException ex) {
             }
         
        Parameters:
        directory - directory to compress
        includeRootDirectory - indicates whether to include the root directory itself or not
        Returns:
        Xar entry instance
        Throws:
        com.aspose.ms.System.ArgumentNullException - directory is null.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access directory.
        com.aspose.ms.System.IO.IOException - directory stands for a file, not for a directory.
      • createEntries

        public final XarArchive createEntries​(File directory,
                                              boolean includeRootDirectory,
                                              XarCompressionSettings compressionSettings)

        Adds to the archive all the files and directories recursively in the directory given.

        
             try (FileOutputStream xarFile = new FileOutputStream("archive.xar")) {
                 try (XarArchive archive = new XarArchive()) {
                     archive.createEntries(new java.io.File("C:\\folder"), false);
                     archive.save(xarFile);
                 }
             } catch (IOException ex) {
             }
         
        Parameters:
        directory - directory to compress
        includeRootDirectory - indicates whether to include the root directory itself or not
        compressionSettings - the compression settings used for added XarEntry items
        Returns:
        Xar entry instance
        Throws:
        com.aspose.ms.System.ArgumentNullException - directory is null.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access directory.
        com.aspose.ms.System.IO.IOException - directory stands for a file, not for a directory.
      • createEntry

        public final XarEntry createEntry​(String name,
                                          File file)

        Create a single entry within the archive.

        
             java.io.File file = new java.io.File("data.bin");
             try (XarArchive archive = new XarArchive()) {
                 archive.createEntry("test.bin", file);
                 archive.save("archive.xar");
             }
         
        Parameters:
        name - the name of the entry
        file - the metadata of file or folder to be compressed
        Returns:
        Xar entry instance
        Throws:
        com.aspose.ms.System.ArgumentNullException - name is null.
        com.aspose.ms.System.ArgumentException - name is empty.
        com.aspose.ms.System.ArgumentNullException - file is null.
      • createEntry

        public final XarEntry createEntry​(String name,
                                          File file,
                                          boolean openImmediately)

        Create a single entry within the archive.

        
             java.io.File file = new java.io.File("data.bin");
             try (XarArchive archive = new XarArchive()) {
                 archive.createEntry("test.bin", file);
                 archive.save("archive.xar");
             }
         

        If the file is opened immediately with openImmediately parameter it becomes blocked until archive is disposed

        Parameters:
        name - the name of the entry
        file - the metadata of file or folder to be compressed
        openImmediately - true if open the file immediately, otherwise open the file on archive saving.
        Returns:
        Xar entry instance
        Throws:
        com.aspose.ms.System.ArgumentNullException - name is null.
        com.aspose.ms.System.ArgumentException - name is empty.
        com.aspose.ms.System.ArgumentNullException - file is null.
      • createEntry

        public final XarEntry createEntry​(String name,
                                          File file,
                                          boolean openImmediately,
                                          XarCompressionSettings compressionSettings)

        Create a single entry within the archive.

        
             java.io.File file = new java.io.File("data.bin");
             try (XarArchive archive = new XarArchive()) {
                 archive.createEntry("test.bin", file);
                 archive.save("archive.xar");
             }
         

        If the file is opened immediately with openImmediately parameter it becomes blocked until archive is disposed

        Parameters:
        name - the name of the entry
        file - the metadata of file or folder to be compressed
        openImmediately - true, if open the file immediately, otherwise open the file on archive saving.
        compressionSettings - the compression settings used for added XarEntry item
        Returns:
        Xar entry instance
        Throws:
        com.aspose.ms.System.ArgumentNullException - name is null.
        com.aspose.ms.System.ArgumentException - name is empty.
        com.aspose.ms.System.ArgumentNullException - file is null.
      • createEntry

        public final XarEntry createEntry​(String name,
                                          String sourcePath)

        Create a single entry within the archive.

        
             try (XarArchive archive = new XarArchive()) {
                 archive.createEntry("first.bin", "data.bin");
                 archive.save("archive.xar");
             }
         

        The entry name is solely set within name parameter. The file name provided in sourcePath parameter does not affect the entry name.

        Parameters:
        name - the name of the entry
        sourcePath - the path to the file to be compressed
        Returns:
        Xar entry instance
        Throws:
        com.aspose.ms.System.ArgumentNullException - sourcePath is null.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access.
        com.aspose.ms.System.ArgumentException - The sourcePath is empty, contains only white spaces, or contains invalid characters, file name, as a part of name, exceeds 100 symbols.
        com.aspose.ms.System.IO.PathTooLongException - The specified sourcePath, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters, name is too long for xar.
        com.aspose.ms.System.NotSupportedException - File at sourcePath contains a colon (:) in the middle of the string.
        com.aspose.ms.System.InvalidOperationException - Impossible to modify xar archive.
      • createEntry

        public final XarEntry createEntry​(String name,
                                          String sourcePath,
                                          boolean openImmediately)

        Create a single entry within the archive.

        
             try (XarArchive archive = new XarArchive()) {
                 archive.createEntry("first.bin", "data.bin");
                 archive.save("archive.xar");
             }
         

        The entry name is solely set within name parameter. The file name provided in sourcePath parameter does not affect the entry name.

        If the file is opened immediately with openImmediately parameter it becomes blocked until archive is disposed.

        Parameters:
        name - the name of the entry
        sourcePath - the path to the file to be compressed
        openImmediately - true, if open the file immediately, otherwise open the file on archive saving
        Returns:
        Xar entry instance
        Throws:
        com.aspose.ms.System.ArgumentNullException - sourcePath is null.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access.
        com.aspose.ms.System.ArgumentException - The sourcePath is empty, contains only white spaces, or contains invalid characters, file name, as a part of name, exceeds 100 symbols.
        com.aspose.ms.System.IO.PathTooLongException - The specified sourcePath, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters, name is too long for xar.
        com.aspose.ms.System.NotSupportedException - File at sourcePath contains a colon (:) in the middle of the string.
        com.aspose.ms.System.InvalidOperationException - Impossible to modify xar archive.
      • createEntry

        public final XarEntry createEntry​(String name,
                                          String sourcePath,
                                          boolean openImmediately,
                                          XarCompressionSettings compressionSettings)

        Create a single entry within the archive.

        
             try (XarArchive archive = new XarArchive()) {
                 archive.createEntry("first.bin", "data.bin");
                 archive.save("archive.xar");
             }
         

        The entry name is solely set within name parameter. The file name provided in sourcePath parameter does not affect the entry name.

        If the file is opened immediately with openImmediately parameter it becomes blocked until archive is disposed.

        Parameters:
        name - the name of the entry
        sourcePath - the path to the file to be compressed
        openImmediately - true, if open the file immediately, otherwise open the file on archive saving
        compressionSettings - the compression settings used for added XarEntry item
        Returns:
        Xar entry instance
        Throws:
        com.aspose.ms.System.ArgumentNullException - sourcePath is null.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access.
        com.aspose.ms.System.ArgumentException - The sourcePath is empty, contains only white spaces, or contains invalid characters, or file name, as a part of name, exceeds 100 symbols.
        com.aspose.ms.System.IO.PathTooLongException - The specified sourcePath, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters, or name is too long for xar.
        com.aspose.ms.System.NotSupportedException - File at sourcePath contains a colon (:) in the middle of the string.
        com.aspose.ms.System.InvalidOperationException - Impossible to modify xar archive.
      • createEntry

        public final XarEntry createEntry​(String name,
                                          InputStream source)

        Create a single entry within the archive.

        
             try (XarArchive archive = new XarArchive()) {
                 archive.createEntry("data.bin", new FileInputStream("data.bin"));
                 archive.save("archive.xar");
             } catch (IOException ex) {
             }
         
        Parameters:
        name - the name of the entry
        source - the input stream for the entry
        Returns:
        Xar entry instance
        Throws:
        com.aspose.ms.System.ArgumentNullException - name is null.
        com.aspose.ms.System.ArgumentNullException - source is null.
        com.aspose.ms.System.ArgumentException - name is empty.
        com.aspose.ms.System.InvalidOperationException - Impossible to modify xar archive.
      • createEntry

        public final XarEntry createEntry​(String name,
                                          InputStream source,
                                          XarCompressionSettings compressionSettings)

        Create a single entry within the archive.

        
             try (XarArchive archive = new XarArchive()) {
                 archive.createEntry("data.bin", new FileInputStream("data.bin"));
                 archive.save("archive.xar");
             } catch (IOException ex) {
             }
         
        Parameters:
        name - the name of the entry
        source - the input stream for the entry
        compressionSettings - the compression settings used for added XarEntry item
        Returns:
        Xar entry instance
        Throws:
        com.aspose.ms.System.ArgumentNullException - name is null.
        com.aspose.ms.System.ArgumentNullException - source is null.
        com.aspose.ms.System.ArgumentException - name is empty.
        com.aspose.ms.System.InvalidOperationException - Impossible to modify xar archive.
      • save

        public final void save​(String destinationFileName)

        Saves archive to the destination file provided.

        Parameters:
        destinationFileName - the path of the archive to be created. If the specified file name points to an existing file, it will be overwritten
        Throws:
        com.aspose.ms.System.ArgumentNullException - destinationFileName is null.
        com.aspose.ms.System.InvalidOperationException - Impossible to modify xar archive.
      • save

        public final void save​(String destinationFileName,
                               XarSaveOptions saveOptions)

        Saves archive to the destination file provided.

        Parameters:
        destinationFileName - the path of the archive to be created. If the specified file name points to an existing file, it will be overwritten
        saveOptions - the options to save xar archive with
        Throws:
        com.aspose.ms.System.ArgumentNullException - destinationFileName is null.
        com.aspose.ms.System.InvalidOperationException - Impossible to modify xar archive.
      • save

        public final void save​(OutputStream output)

        Saves archive to the stream provided.

        For large archives use save(String) instead of saving to FileOutputStream.
        Parameters:
        output - the destination stream
        Throws:
        com.aspose.ms.System.ArgumentNullException - output is null.
        com.aspose.ms.System.InvalidOperationException - Impossible to modify xar archive.
      • save

        public final void save​(OutputStream output,
                               XarSaveOptions saveOptions)

        Saves archive to the stream provided.

        For large archives use save(String) instead of saving to FileOutputStream.
        Parameters:
        output - the destination stream
        saveOptions - the options to save xar archive with
        Throws:
        com.aspose.ms.System.ArgumentNullException - output is null.
        com.aspose.ms.System.InvalidOperationException - Impossible to modify xar archive.
      • deleteEntry

        public final XarArchive deleteEntry​(XarEntry entry)

        Removes the first occurrence of a specific entry from the entry list.

        Here is how you can remove all entries except the last one:

        
             try (XarArchive archive = new XarArchive("archive.xar")) {
                 while (archive.getEntries().size() > 1)
                     archive.deleteEntry(archive.getEntries().get(0));
                 archive.save("outputXarFile.xar");
             }
         
        Parameters:
        entry - the entry to remove from the entries list
        Returns:
        Xar entry instance
        Throws:
        com.aspose.ms.System.ArgumentNullException - entry is null.