Class SharArchive

Class SharArchive

Namespace: Aspose.Zip.Shar
Assembly: Aspose.Zip.dll (25.2.0)

This class represents shar archive file.

public class SharArchive : IDisposable

Inheritance

objectSharArchive

Implements

IDisposable

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

ArgumentNullException

path is null.

SecurityException

The caller does not have the required permission to access.

ArgumentException

The path is empty, contains only white spaces, or contains invalid characters.

UnauthorizedAccessException

Access to file path is denied.

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.

NotSupportedException

File at path contains a colon (:) in the middle of the string.

FileNotFoundException

The file is not found.

DirectoryNotFoundException

The specified path is invalid, such as being on an unmapped drive.

IOException

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&gt;

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

SharArchive

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

ArgumentNullException

sourceDirectory is null.

SecurityException

The caller does not have the required permission to access sourceDirectory.

ArgumentException

sourceDirectory contains invalid characters such as “, <, >, or |.

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.

IOException

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

SharArchive

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

ArgumentNullException

directory is null.

SecurityException

The caller does not have the required permission to access directory.

IOException

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

SharEntry

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

ArgumentNullException

name is null.

ArgumentException

name is empty.

ArgumentNullException

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

SharEntry

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

ArgumentNullException

sourcePath is null.

SecurityException

The caller does not have the required permission to access.

ArgumentException

The sourcePath is empty, contains only white spaces, or contains invalid characters. - or - File name, as a part of name, exceeds 100 symbols.

UnauthorizedAccessException

Access to file sourcePath is denied.

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.

NotSupportedException

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

SharEntry

Shar entry instance.

Examples

using (var archive = new SharArchive())
{
    archive.CreateEntry("data.bin", File.OpenRead("data.bin"));
    archive.Save("archive.shar");
}

Exceptions

ArgumentNullException

name is null.

ArgumentNullException

source is null.

ArgumentException

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

SharArchive

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 &gt; 1)
        archive.DeleteEntry(archive.Entries[0]);
    archive.Save(outputSharFile);
}

Exceptions

ArgumentNullException

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

SharArchive

The archive with the entry deleted.

Examples

using (var archive = new SharArchive("two_files.shar"))
{
    archive.DeleteEntry(0);
    archive.Save("single_file.shar");
}

Exceptions

ArgumentOutOfRangeException

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

ArgumentException

destinationFileName is a zero-length string, contains only white space, or contains one or more invalid characters as defined by System.IO.Path.InvalidPathChars.

ArgumentNullException

destinationFileName is null.

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.

DirectoryNotFoundException

The specified destinationFileName is invalid, (for example, it is on an unmapped drive).

IOException

An I/O error occurred while opening the file.

UnauthorizedAccessException

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.

NotSupportedException

destinationFileName is in an invalid format.

FileNotFoundException

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

ArgumentNullException

output is null.

ArgumentException

output is not writable. - or - output is the same stream we extract from.

 English