Class SharArchive
- java.lang.Object
-
- com.aspose.zip.SharArchive
-
- All Implemented Interfaces:
AutoCloseable
public class SharArchive extends Object implements AutoCloseable
This class represents a shar archive file.
-
-
Constructor Summary
Constructors Constructor Description SharArchive()Initializes a new instance of theSharArchiveclass.SharArchive(String path)Initializes a new instance of theSharArchiveclass prepared for decompressing.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()SharArchivecreateEntries(File directory)Adds to the archive all the files and directories recursively in the directory given.SharArchivecreateEntries(File directory, boolean includeRootDirectory)Adds to the archive all the files and directories recursively in the directory given.SharArchivecreateEntries(String sourceDirectory)Adds to the archive all the files and directories recursively in the directory given.SharArchivecreateEntries(String sourceDirectory, boolean includeRootDirectory)Adds to the archive all the files and directories recursively in the directory given.SharEntrycreateEntry(String name, File file)Creates a single entry within the archive.SharEntrycreateEntry(String name, File file, boolean includeRootDirectory)Create a single entry within the archive.SharEntrycreateEntry(String name, InputStream source)Create a single entry within the archive.SharEntrycreateEntry(String name, String sourcePath)Create a single entry within the archive.SharEntrycreateEntry(String name, String sourcePath, boolean openImmediately)Create a single entry within the archive.SharArchivedeleteEntry(int entryIndex)Removes the entry from the entry list by index.SharArchivedeleteEntry(SharEntry entry)Removes the first occurrence of a specific entry from the entry list.List<SharEntry>getEntries()Gets entries ofSharEntrytype constituting the archive.voidsave(OutputStream output)Saves archive to the stream provided.voidsave(String destinationFileName)Saves archive to the destination file provided.
-
-
-
Constructor Detail
-
SharArchive
public SharArchive()
Initializes a new instance of the
SharArchiveclass.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
SharArchiveclass prepared for decompressing.- Parameters:
path- the path to the source of the archive- Throws:
com.aspose.ms.System.ArgumentNullException-pathis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to access.com.aspose.ms.System.ArgumentException- Thepathis empty, contains only white spaces, or contains invalid characters.com.aspose.ms.System.IO.PathTooLongException- The specifiedpath, 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 atpathcontains 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
SharEntrytype constituting the archive.- Returns:
- entries of
SharEntrytype 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-sourceDirectoryis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to accesssourceDirectory.com.aspose.ms.System.ArgumentException-sourceDirectorycontains 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-sourceDirectorystands 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 compressincludeRootDirectory- indicates whether to include the root directory itself or not- Returns:
- Shar entry instance
- Throws:
com.aspose.ms.System.ArgumentNullException-sourceDirectoryis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to accesssourceDirectory.com.aspose.ms.System.ArgumentException-sourceDirectorycontains 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-sourceDirectorystands 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-directoryis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to accessdirectory.com.aspose.ms.System.IO.IOException-directorystands 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 compressincludeRootDirectory- indicates whether to include the root directory itself or not- Returns:
- Shar entry instance
- Throws:
com.aspose.ms.System.ArgumentNullException-directoryis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to accessdirectory.com.aspose.ms.System.IO.IOException-directorystands 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 entryfile- the metadata of file or folder to be compressed- Returns:
- Shar entry instance
- Throws:
com.aspose.ms.System.ArgumentNullException-nameis null.com.aspose.ms.System.ArgumentException-nameis empty.com.aspose.ms.System.ArgumentNullException-fileis 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 entryfile- the metadata of file or folder to be compressedincludeRootDirectory- indicates whether to include the root directory itself or not- Returns:
- Shar entry instance
- Throws:
com.aspose.ms.System.ArgumentNullException-nameis null.com.aspose.ms.System.ArgumentException-nameis empty.com.aspose.ms.System.ArgumentNullException-fileis 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
nameparameter. The file name provided insourcePathparameter does not affect the entry name.- Parameters:
name- the name of the entrysourcePath- the path to the file to be compressed- Returns:
- Shar entry instance
- Throws:
com.aspose.ms.System.ArgumentNullException-sourcePathis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to access.com.aspose.ms.System.ArgumentException- ThesourcePathis empty, contains only white spaces, or contains invalid characters. - or - File name, as a part ofname, exceeds 100 symbols.com.aspose.ms.System.IO.PathTooLongException- The specifiedsourcePath, 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 -nameis too long for shar.com.aspose.ms.System.NotSupportedException- File atsourcePathcontains 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
nameparameter. The file name provided insourcePathparameter does not affect the entry name.If the file is opened immediately with
openImmediatelyparameter it becomes blocked until archive is disposed.- Parameters:
name- the name of the entrysourcePath- the path to file to be compressedopenImmediately- true, if open the file immediately, otherwise open the file on archive saving- Returns:
- Shar entry instance
- Throws:
com.aspose.ms.System.ArgumentNullException-sourcePathis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to access.com.aspose.ms.System.ArgumentException- ThesourcePathis empty, contains only white spaces, or contains invalid characters. - or - File name, as a part ofname, exceeds 100 symbols.com.aspose.ms.System.IO.PathTooLongException- The specifiedsourcePath, 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 -nameis too long for shar.com.aspose.ms.System.NotSupportedException- File atsourcePathcontains 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 entrysource- the input stream for the entry- Returns:
- Shar entry instance
- Throws:
com.aspose.ms.System.ArgumentNullException-nameis null.com.aspose.ms.System.ArgumentNullException-sourceis null.com.aspose.ms.System.ArgumentException-nameis 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-entryis 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-entryIndexis less than 0.-or-entryIndexis equal to or greater thanEntriescount.
-
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-destinationFileNameis a zero-length string, contains only white space, or contains one or more invalid characters.com.aspose.ms.System.ArgumentNullException-destinationFileNameis null.com.aspose.ms.System.IO.PathTooLongException- The specifieddestinationFileName, 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 specifieddestinationFileNameis 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-destinationFileNameis 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-outputis null.com.aspose.ms.System.ArgumentException-outputis the same stream we extract from.
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable
-
-