Class SharArchive

    • Constructor Detail

      • SharArchive

        public SharArchive()

        Initializes a new instance of the SharArchive class.

        The following example shows how to compress a file.

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

        public SharArchive​(String path)

        Initializes a new instance of the SharArchive class prepared for decompressing.

        Parameters:
        path - the path to the source of the archive
        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.
    • Method Detail

      • getEntries

        public final List<SharEntry> getEntries()

        Gets entries of SharEntry type constituting the archive.

        Returns:
        entries of SharEntry type constituting the archive
      • createEntries

        public final SharArchive createEntries​(String sourceDirectory)

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

        
             try (FileOutputStream sharFile = new FileOutputStream("archive.shar")) {
                 try (SharArchive archive = new SharArchive()) {
                     archive.createEntries("C:\\folder", false);
                     archive.save(sharFile);
                 }
             } catch (IOException ex) {
             }
         
        Parameters:
        sourceDirectory - the directory to compress
        Returns:
        Shar 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 SharArchive createEntries​(String sourceDirectory,
                                               boolean includeRootDirectory)

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

        
             try (FileOutputStream sharFile = new FileOutputStream("archive.shar")) {
                 try (SharArchive archive = new SharArchive()) {
                     archive.createEntries("C:\\folder", false);
                     archive.save(sharFile);
                 }
             } catch (IOException ex) {
             }
         
        Parameters:
        sourceDirectory - the directory to compress
        includeRootDirectory - indicates whether to include the root directory itself or not
        Returns:
        Shar 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 SharArchive createEntries​(File directory)

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

        
             try (FileOutputStream sharFile = new FileOutputStream("archive.shar")) {
                 try (SharArchive archive = new SharArchive()) {
                     archive.createEntries(new java.io.File("C:\\folder"), false);
                     archive.save(sharFile);
                 }
             } catch (IOException ex) {
             }
         
        Parameters:
        directory - the directory to compress
        Returns:
        Shar 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 SharArchive createEntries​(File directory,
                                               boolean includeRootDirectory)

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

        
             try (FileOutputStream sharFile = new FileOutputStream("archive.shar")) {
                 try (SharArchive archive = new SharArchive()) {
                     archive.createEntries(new java.io.File("C:\\folder"), false);
                     archive.save(sharFile);
                 }
             } catch (IOException ex) {
             }
         
        Parameters:
        directory - the directory to compress
        includeRootDirectory - indicates whether to include the root directory itself or not
        Returns:
        Shar 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 SharEntry createEntry​(String name,
                                           File file)

        Creates a single entry within the archive.

        
             java.io.File file = new java.io.File("data.bin");
             try (SharArchive archive = new SharArchive()) {
                 archive.createEntry("test.bin", file);
                 archive.save("archive.shar");
             }
         
        Parameters:
        name - the name of the entry
        file - the metadata of file or folder to be compressed
        Returns:
        Shar 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 SharEntry createEntry​(String name,
                                           File file,
                                           boolean includeRootDirectory)

        Create a single entry within the archive.

        
             java.io.File file = new java.io.File("data.bin");
             try (SharArchive archive = new SharArchive()) {
                 archive.createEntry("test.bin", file);
                 archive.save("archive.shar");
             }
         
        Parameters:
        name - the name of the entry
        file - the metadata of file or folder to be compressed
        includeRootDirectory - indicates whether to include the root directory itself or not
        Returns:
        Shar 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 SharEntry createEntry​(String name,
                                           String sourcePath)

        Create a single entry within the archive.

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

        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:
        Shar 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 shar.
        com.aspose.ms.System.NotSupportedException - File at sourcePath contains a colon (:) in the middle of the string.
      • createEntry

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

        Create a single entry within the archive.

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

        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 file to be compressed
        openImmediately - true, if open the file immediately, otherwise open the file on archive saving
        Returns:
        Shar 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 shar.
        com.aspose.ms.System.NotSupportedException - File at sourcePath contains a colon (:) in the middle of the string.
      • createEntry

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

        Create a single entry within the archive.

        
             try (SharArchive archive = new SharArchive()) {
                 archive.createEntry("data.bin", new FileInputStream("data.bin"));
                 archive.save("archive.shar");
             } catch (IOException ex) {
             }
         
        Parameters:
        name - the name of the entry
        source - the input stream for the entry
        Returns:
        Shar 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.
      • deleteEntry

        public final SharArchive deleteEntry​(SharEntry 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 (SharArchive archive = new SharArchive("archive.shar")) {
                 while (archive.getEntries().size() > 1)
                     archive.deleteEntry(archive.getEntries().get(0));
                 archive.save("outputSharFile.shar");
             }
         
        Parameters:
        entry - the entry to remove from the entries list
        Returns:
        Shar entry instance
        Throws:
        com.aspose.ms.System.ArgumentNullException - entry is null.
      • deleteEntry

        public final SharArchive deleteEntry​(int entryIndex)

        Removes the entry from the entry list by index.

        
             try (SharArchive archive = new SharArchive("two_files.shar")) {
                 archive.deleteEntry(0);
                 archive.save("single_file.shar");
             }
         
        Parameters:
        entryIndex - the zero-based index of the entry to remove
        Returns:
        the archive with the entry deleted
        Throws:
        com.aspose.ms.System.ArgumentOutOfRangeException - entryIndex is less than 0.-or- entryIndex is equal to or greater than Entries count.
      • save

        public final void save​(String destinationFileName)

        Saves archive to the destination file provided.

        
             try (SharArchive archive = new SharArchive()) {
                 archive.createEntry("entry1", "data.bin");
                 archive.save("archive.shar");
             }
         
        Parameters:
        destinationFileName - the path of the archive to be created. If the specified file name points to an existing file, it will be overwritten.

        It is possible to save an archive to the same path as it was loaded from. However, this is not recommended because this approach uses copying to a temporary file

        Throws:
        com.aspose.ms.System.ArgumentException - destinationFileName is a zero-length string, contains only white space, or contains one or more invalid characters.
        com.aspose.ms.System.ArgumentNullException - destinationFileName is null.
        com.aspose.ms.System.IO.PathTooLongException - The specified destinationFileName, 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.IO.DirectoryNotFoundException - The specified destinationFileName is invalid, (for example, it is on an unmapped drive).
        com.aspose.ms.System.IO.IOException - An I/O error occurred while opening the file.
        com.aspose.ms.System.NotSupportedException - destinationFileName is in an invalid format.
        com.aspose.ms.System.IO.FileNotFoundException - The file is not found.
      • save

        public final void save​(OutputStream output)

        Saves archive to the stream provided.

        
             try (FileOutputStream sharFile = new FileOutputStream("archive.shar")) {
                 try (SharArchive archive = new SharArchive()) {
                     archive.createEntry("entry1", "data.bin");
                     archive.save(sharFile);
                 }
             } catch (IOException ex) {
             }
         
        Parameters:
        output - the destination stream
        Throws:
        com.aspose.ms.System.ArgumentNullException - output is null.
        com.aspose.ms.System.ArgumentException - output is the same stream we extract from.