Class XarArchive

Class XarArchive

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

This class represents xar archive file.

public class XarArchive : IArchive, IDisposable

Inheritance

objectXarArchive

Implements

IArchive, IDisposable

Inherited Members

object.GetType(), object.MemberwiseClone(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

Constructors

XarArchive(XarCompressionSettings)

Initializes a new instance of the Aspose.Zip.Xar.XarArchive class.

public XarArchive(XarCompressionSettings defaultCompressionSettings = null)

Parameters

defaultCompressionSettings XarCompressionSettings

The default compression settings, applyed to all entries of the archive.

Examples

The following example shows how to compress a file.

using (var archive = new XarArchive())
{
    archive.CreateEntry("first.bin", "data.bin");
    archive.Save("archive.xar");
}

XarArchive(Stream, XarLoadOptions)

Initializes a new instance of the Aspose.Zip.Xar.XarArchive class and composes entries list can be extracted from the archive.

public XarArchive(Stream sourceStream, XarLoadOptions loadOptions = null)

Parameters

sourceStream Stream

The source of the archive. It must be seekable.

loadOptions XarLoadOptions

The options to load archive with.

Examples

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

using (var archive = new XarArchive(File.OpenRead("archive.xar")))
{
   archive.ExtractToDirectory("C:\\extracted");
}

Remarks

This constructor does not unpack any entry. See Aspose.Zip.Xar.XarFileEntry.Open method for unpacking.

Exceptions

ArgumentNullException

sourceStream is null.

ArgumentException

sourceStream is not seekable.

InvalidDataException

sourceStream is not valid xar archive.

XarArchive(string, XarLoadOptions)

Initializes a new instance of the Aspose.Zip.Xar.XarArchive class and composes entries list can be extracted from the archive.

public XarArchive(string path, XarLoadOptions loadOptions = null)

Parameters

path string

The path to the archive file.

loadOptions XarLoadOptions

The options to load archive with.

Examples

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

using (var archive = new XarArchive("archive.xar")) 
{
   archive.ExtractToDirectory("C:\\extracted");
}

Remarks

This constructor does not unpack any entry. See Aspose.Zip.Xar.XarFileEntry.Open method for unpacking.

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.

InvalidDataException

File at path is not valid xar archive.

Properties

Entries

Gets entries of Aspose.Zip.Xar.XarEntry type constituting the archive.

public IEnumerable<xarentry> Entries { get; }

Property Value

IEnumerable<XarEntry&gt;

Methods

CreateEntries(string, bool, XarCompressionSettings)

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

public XarArchive CreateEntries(string sourceDirectory, bool includeRootDirectory = true, XarCompressionSettings compressionSettings = null)

Parameters

sourceDirectory string

Directory to compress.

includeRootDirectory bool

Indicates whether to include the root directory itself or not.

compressionSettings XarCompressionSettings

The compression settings used for added Aspose.Zip.Xar.XarEntry items.

Returns

XarArchive

Xar entry instance.

Examples

using (FileStream xarFile = File.Open("archive.xar", FileMode.Create))
{
    using (var archive = new XarArchive())
    {
        archive.CreateEntries(@"C:\folder", false);
        archive.Save(xarFile);
    }
}

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, XarCompressionSettings)

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

public XarArchive CreateEntries(DirectoryInfo directory, bool includeRootDirectory = true, XarCompressionSettings compressionSettings = null)

Parameters

directory DirectoryInfo

Directory to compress.

includeRootDirectory bool

Indicates whether to include the root directory itself or not.

compressionSettings XarCompressionSettings

The compression settings used for added Aspose.Zip.Xar.XarEntry items.

Returns

XarArchive

Xar entry instance.

Examples

using (FileStream xarFile = File.Open("archive.xar", FileMode.Create))
{
    using (var archive = new XarArchive())
    {
        archive.CreateEntries(new DirectoryInfo(@"C:\folder"), false);
        archive.Save(xarFile);
    }
}

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, XarCompressionSettings)

Create single entry within the archive.

public XarEntry CreateEntry(string name, FileInfo fileInfo, bool openImmediately = false, XarCompressionSettings compressionSettings = null)

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.

compressionSettings XarCompressionSettings

The compression settings used for added Aspose.Zip.Xar.XarEntry item.

Returns

XarEntry

Xar entry instance.

Examples

FileInfo fileInfo = new FileInfo("data.bin");
using (var archive = new XarArchive())
{
    archive.CreateEntry("test.bin", fileInfo);
    archive.Save("archive.xar");
}

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, XarCompressionSettings)

Create single entry within the archive.

public XarEntry CreateEntry(string name, string sourcePath, bool openImmediately = false, XarCompressionSettings compressionSettings = null)

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.

compressionSettings XarCompressionSettings

The compression settings used for added Aspose.Zip.Xar.XarEntry item.

Returns

XarEntry

Xar entry instance.

Examples

using (var archive = new XarArchive())
{
    archive.CreateEntry("first.bin", "data.bin");
    archive.Save("archive.xar");
}

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 xar.

NotSupportedException

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

InvalidOperationException

Impossible to modify xar archive.

CreateEntry(string, Stream, XarCompressionSettings)

Create single entry within the archive.

public XarEntry CreateEntry(string name, Stream source, XarCompressionSettings compressionSettings = null)

Parameters

name string

The name of the entry.

source Stream

The input stream for the entry.

compressionSettings XarCompressionSettings

The compression settings used for added Aspose.Zip.Xar.XarEntry item.

Returns

XarEntry

Xar entry instance.

Examples

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

Exceptions

ArgumentNullException

name is null.

ArgumentNullException

source is null.

ArgumentException

name is empty.

InvalidOperationException

Impossible to modify xar archive.

DeleteEntry(XarEntry)

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

public XarArchive DeleteEntry(XarEntry entry)

Parameters

entry XarEntry

The entry to remove from the entries list.

Returns

XarArchive

Xar entry instance.

Examples

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

using (var archive = new XarArchive("archive.xar"))
{
    while (archive.Entries.Count &gt; 1)
        archive.DeleteEntry(archive.Entries.FirstOrDefault());
    archive.Save(outputXarFile);
}

Exceptions

ArgumentNullException

entry is null.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

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.

ExtractToDirectory(string)

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

public void ExtractToDirectory(string destinationDirectory)

Parameters

destinationDirectory string

The path to the directory to place the extracted files in.

Examples

using (var archive = new XarArchive("archive.xar")) 
{
   archive.ExtractToDirectory("C:\\extracted");
}

Remarks

If the directory does not exist, it will be created.

Exceptions

ArgumentNullException

path is null

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.

SecurityException

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

NotSupportedException

If directory does not exist, path contains a colon character (:) that is not part of a drive label (“C:").

ArgumentException

path is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the System.IO.Path.GetInvalidPathChars method. -or- path is prefixed with, or contains, only a colon character (:).

IOException

The directory specified by path is a file. -or- The network name is not known.

InvalidDataException

Archive is corrupted.

Save(string, XarSaveOptions)

Saves archive to destination file provided.

public void Save(string destinationFileName, XarSaveOptions saveOptions = null)

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.

saveOptions XarSaveOptions

Options to save xar archive with.

Exceptions

ArgumentNullException

destinationFileName is null.

InvalidOperationException

Impossible to modify xar archive.

Save(Stream, XarSaveOptions)

Saves archive to the stream provided.

public void Save(Stream output, XarSaveOptions saveOptions = null)

Parameters

output Stream

Destination stream.

saveOptions XarSaveOptions

Options to save xar archive with.

Exceptions

ArgumentNullException

output is null.

ArgumentException

outputIs not writable/readable or not seekable.

InvalidOperationException

Impossible to modify xar archive.

 English