Class SharArchive
Namespace: Aspose.Zip.Shar
Assembly: Aspose.Zip.dll (25.2.0)
This class represents shar archive file.
public class SharArchive : IDisposable
Inheritance
Implements
Inherited Members
object.GetType(), object.MemberwiseClone(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()
Constructors
SharArchive()
Initializes a new instance of the Aspose.Zip.Shar.SharArchive class.
public SharArchive()
Examples
The following example shows how to compress a file.
using (var archive = new SharArchive())
{
archive.CreateEntry("first.bin", "data.bin");
archive.Save("archive.shar");
}
SharArchive(string)
Initializes a new instance of the Aspose.Zip.Shar.SharArchive class prepared for decompressing.
public SharArchive(string path)
Parameters
path
string
Path to the source of the archive.
Exceptions
path
is null.
The caller does not have the required permission to access.
The path
is empty, contains only white spaces, or contains invalid characters.
Access to file path
is denied.
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.
File at path
contains a colon (:) in the middle of the string.
The file is not found.
The specified path is invalid, such as being on an unmapped drive.
The file is already open.
Properties
Entries
Gets entries of Aspose.Zip.Shar.SharEntry type constituting the archive.
public ReadOnlyCollection<sharentry> Entries { get; }
Property Value
ReadOnlyCollection<SharEntry>
Methods
CreateEntries(string, bool)
Adds to the archive all the files and directories recursively in the directory given.
public SharArchive CreateEntries(string sourceDirectory, bool includeRootDirectory = true)
Parameters
sourceDirectory
string
Directory to compress.
includeRootDirectory
bool
Indicates whether to include the root directory itself or not.
Returns
Shar entry instance.
Examples
using (FileStream sharFile = File.Open("archive.shar", FileMode.Create))
{
using (var archive = new SharArchive())
{
archive.CreateEntries("C:\folder", false);
archive.Save(sharFile);
}
}
Exceptions
sourceDirectory
is null.
The caller does not have the required permission to access sourceDirectory
.
sourceDirectory
contains invalid characters such as “, <, >, or |.
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.
sourceDirectory
stands for a file, not for a directory.
CreateEntries(DirectoryInfo, bool)
Adds to the archive all the files and directories recursively in the directory given.
public SharArchive CreateEntries(DirectoryInfo directory, bool includeRootDirectory = true)
Parameters
directory
DirectoryInfo
Directory to compress.
includeRootDirectory
bool
Indicates whether to include the root directory itself or not.
Returns
Shar entry instance.
Examples
using (FileStream sharFile = File.Open("archive.shar", FileMode.Create))
{
using (var archive = new SharArchive())
{
archive.CreateEntries(new DirectoryInfo("C:\folder"), false);
archive.Save(sharFile);
}
}
Exceptions
directory
is null.
The caller does not have the required permission to access directory
.
directory
stands for a file, not for a directory.
CreateEntry(string, FileInfo, bool)
Create single entry within the archive.
public SharEntry CreateEntry(string name, FileInfo fileInfo, bool openImmediately = false)
Parameters
name
string
The name of the entry.
fileInfo
FileInfo
The metadata of file or folder to be compressed.
openImmediately
bool
True if open the file immediately, otherwise open the file on archive saving.
Returns
Shar entry instance.
Examples
FileInfo fileInfo = new FileInfo("data.bin");
using (var archive = new SharArchive())
{
archive.CreateEntry("test.bin", fileInfo);
archive.Save("archive.shar");
}
Remarks
If the file is opened immediately with openImmediately
parameter it becomes blocked until archive is disposed.
Exceptions
name
is null.
name
is empty.
fileInfo
is null.
CreateEntry(string, string, bool)
Create single entry within the archive.
public SharEntry CreateEntry(string name, string sourcePath, bool openImmediately = false)
Parameters
name
string
The name of the entry.
sourcePath
string
Path to file to be compressed.
openImmediately
bool
True if open the file immediately, otherwise open the file on archive saving.
Returns
Shar entry instance.
Examples
using (var archive = new SharArchive())
{
archive.CreateEntry("first.bin", "data.bin");
archive.Save("archive.shar");
}
Remarks
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.
Exceptions
sourcePath
is null.
The caller does not have the required permission to access.
The sourcePath
is empty, contains only white spaces, or contains invalid characters. - or - File name, as a part of name
, exceeds 100 symbols.
Access to file sourcePath
is denied.
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.
File at sourcePath
contains a colon (:) in the middle of the string.
CreateEntry(string, Stream)
Create single entry within the archive.
public SharEntry CreateEntry(string name, Stream source)
Parameters
name
string
The name of the entry.
source
Stream
The input stream for the entry.
Returns
Shar entry instance.
Examples
using (var archive = new SharArchive())
{
archive.CreateEntry("data.bin", File.OpenRead("data.bin"));
archive.Save("archive.shar");
}
Exceptions
name
is null.
source
is null.
name
is empty.
DeleteEntry(SharEntry)
Removes the first occurrence of a specific entry from the entries list.
public SharArchive DeleteEntry(SharEntry entry)
Parameters
entry
SharEntry
The entry to remove from the entries list.
Returns
Shar entry instance.
Examples
Here is how you can remove all entries except the last one:
using (var archive = new SharArchive("archive.shar"))
{
while (archive.Entries.Count > 1)
archive.DeleteEntry(archive.Entries[0]);
archive.Save(outputSharFile);
}
Exceptions
entry
is null.
DeleteEntry(int)
Removes the entry from the entries list by index.
public SharArchive DeleteEntry(int entryIndex)
Parameters
entryIndex
int
The zero-based index of the entry to remove.
Returns
The archive with the entry deleted.
Examples
using (var archive = new SharArchive("two_files.shar"))
{
archive.DeleteEntry(0);
archive.Save("single_file.shar");
}
Exceptions
entryIndex
is less than 0.-or- entryIndex
is equal to or greater than Entries
count.
Dispose(bool)
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
protected virtual void Dispose(bool disposing)
Parameters
disposing
bool
Whether managed resources should be disposed.
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Save(string)
Saves archive to destination file provided.
public void Save(string destinationFileName)
Parameters
destinationFileName
string
The path of the archive to be created. If the specified file name points to an existing file, it will be overwritten.
Examples
using (var archive = new SharArchive())
{
archive.CreateEntry("entry1", "data.bin");
archive.Save("archive.shar");
}
Remarks
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 temporary file.
Exceptions
destinationFileName
is a zero-length string, contains only white space, or contains one or more invalid characters as defined by System.IO.Path.InvalidPathChars.
destinationFileName
is null.
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.
The specified destinationFileName
is invalid, (for example, it is on an unmapped drive).
An I/O error occurred while opening the file.
destinationFileName
specified a file that is read-only and access is not Read.-or- path specified a directory.-or- The caller does not have the required permission.
destinationFileName
is in an invalid format.
The file is not found.
Save(Stream)
Saves archive to the stream provided.
public void Save(Stream output)
Parameters
output
Stream
Destination stream.
Examples
using (FileStream sharFile = File.Open("archive.shar", FileMode.Create))
{
using (var archive = new SharArchive())
{
archive.CreateEntry("entry1", "data.bin");
archive.Save(sharFile);
}
}
Remarks
output
must be writable.
Exceptions
output
is null.
output
is not writable. - or - output
is the same stream we extract from.