Class CpioArchive
Namespace: Aspose.Zip.Cpio
Assembly: Aspose.Zip.dll (25.2.0)
This class represents cpio archive file.
public class CpioArchive : IArchive, 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
CpioArchive()
Initializes a new instance of the Aspose.Zip.Cpio.CpioArchive class.
public CpioArchive()
Examples
The following example shows how to compress a file.
using (var archive = new CpioArchive())
{
archive.CreateEntry("first.bin", "data.bin");
archive.Save("archive.cpio");
}
CpioArchive(Stream)
Initializes a new instance of the Aspose.Zip.Cpio.CpioArchive class and composes entries list can be extracted from the archive.
public CpioArchive(Stream sourceStream)
Parameters
sourceStream
Stream
The source of the archive. It must be seekable.
Examples
The following example shows how to extract all of the entries to a directory.
using (var archive = new CpioArchive(File.OpenRead("archive.cpio")))
{
archive.ExtractToDirectory("C:\extracted");
}
Remarks
This constructor does not unpack any entry. See Aspose.Zip.Cpio.CpioEntry.Open method for unpacking.
Exceptions
sourceStream
is null.
sourceStream
is not seekable.
sourceStream
is not valid cpio archive.
CpioArchive(string)
Initializes a new instance of the Aspose.Zip.Cpio.CpioArchive class and composes entries list can be extracted from the archive.
public CpioArchive(string path)
Parameters
path
string
The path to the archive file.
Examples
The following example shows how to extract all of the entries to a directory.
using (var archive = new CpioArchive("archive.cpio"))
{
archive.ExtractToDirectory("C:\extracted");
}
Remarks
This constructor does not unpack any entry. See Aspose.Zip.Cpio.CpioEntry.Open method for unpacking.
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.Cpio.CpioEntry type constituting the archive.
public ReadOnlyCollection<cpioentry> Entries { get; }
Property Value
ReadOnlyCollection<CpioEntry>
Methods
CreateEntries(string, bool)
Adds to the archive all the files and directories recursively in the directory given.
public CpioArchive 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
Cpio entry instance.
Examples
using (FileStream cpioFile = File.Open("archive.cpio", FileMode.Create))
{
using (var archive = new CpioArchive())
{
archive.CreateEntries("C:\folder", false);
archive.Save(cpioFile);
}
}
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 CpioArchive 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
Cpio entry instance.
Examples
using (FileStream cpioFile = File.Open("archive.cpio", FileMode.Create))
{
using (var archive = new CpioArchive())
{
archive.CreateEntries(new DirectoryInfo("C:\folder"), false);
archive.Save(cpioFile);
}
}
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 CpioEntry 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
Cpio entry instance.
Examples
FileInfo fileInfo = new FileInfo("data.bin");
using (var archive = new CpioArchive())
{
archive.CreateEntry("test.bin", fileInfo);
archive.Save("archive.cpio");
}
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 CpioEntry 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
Cpio entry instance.
Examples
using (var archive = new CpioArchive())
{
archive.CreateEntry("first.bin", "data.bin");
archive.Save("archive.cpio");
}
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 cpio.
File at sourcePath
contains a colon (:) in the middle of the string.
CreateEntry(string, Stream)
Create single entry within the archive.
public CpioEntry CreateEntry(string name, Stream source)
Parameters
name
string
The name of the entry.
source
Stream
The input stream for the entry.
Returns
Cpio entry instance.
Examples
using (var archive = new CpioArchive())
{
archive.CreateEntry("data.bin", File.OpenRead("data.bin"));
archive.Save("archive.cpio");
}
Exceptions
name
is null.
source
is null.
name
is empty.
DeleteEntry(CpioEntry)
Removes the first occurrence of a specific entry from the entries list.
public CpioArchive DeleteEntry(CpioEntry entry)
Parameters
entry
CpioEntry
The entry to remove from the entries list.
Returns
Cpio entry instance.
Examples
Here is how you can remove all entries except the last one:
using (var archive = new CpioArchive("archive.cpio"))
{
while (archive.Entries.Count > 1)
archive.DeleteEntry(archive.Entries[0]);
archive.Save(outputCpioFile);
}
Exceptions
entry
is null.
DeleteEntry(int)
Removes the entry from the entries list by index.
public CpioArchive 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 CpioArchive("two_files.cpio"))
{
archive.DeleteEntry(0);
archive.Save("single_file.cpio");
}
Exceptions
entryIndex
is less than 0.-or- entryIndex
is equal to or greater than Entries
count.
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 CpioArchive("archive.cpio"))
{
archive.ExtractToDirectory("C:\extracted");
}
Remarks
If the directory does not exist, it will be created.
Exceptions
path is null
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 caller does not have the required permission to access existing directory.
If directory does not exist, path contains a colon character (:) that is not part of a drive label (“C:").
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 (:).
The directory specified by path is a file. -or- The network name is not known.
Save(string, CpioFormat)
Saves archive to destination file provided.
public void Save(string destinationFileName, CpioFormat cpioFormat = CpioFormat.OldAscii)
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.
cpioFormat
CpioFormat
Defines cpio header format.
Examples
using (var archive = new CpioArchive())
{
archive.CreateEntry("entry1", "data.bin");
archive.Save("archive.cpio");
}
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.
Save(Stream, CpioFormat)
Saves archive to the stream provided.
public void Save(Stream output, CpioFormat cpioFormat = CpioFormat.OldAscii)
Parameters
output
Stream
Destination stream.
cpioFormat
CpioFormat
Defines cpio header format.
Examples
using (FileStream cpioFile = File.Open("archive.cpio", FileMode.Create))
{
using (var archive = new CpioArchive())
{
archive.CreateEntry("entry1", "data.bin");
archive.Save(cpioFile);
}
}
Remarks
output
must be writable.
Exceptions
output
is null.
output
is not writable. - or - output
is the same stream we extract from.
- OR -
It is impossible to save archive in cpioFormat
due to format restrictions.
SaveGzipped(Stream, CpioFormat)
Saves archive to the stream with gzip compression.
public void SaveGzipped(Stream output, CpioFormat cpioFormat = CpioFormat.OldAscii)
Parameters
output
Stream
Destination stream.
cpioFormat
CpioFormat
Defines cpio header format.
Examples
using (FileStream result = File.OpenWrite("result.cpio.gz"))
{
using (FileStream source = File.Open("data.bin", FileMode.Open, FileAccess.Read))
{
using (var archive = new CpioArchive())
{
archive.CreateEntry("entry.bin", source);
archive.SaveGzipped(result);
}
}
}
Remarks
output
must be writable.
Exceptions
output
is null.
output
is not writable.
SaveGzipped(string, CpioFormat)
Saves archive to the file by path with gzip compression.
public void SaveGzipped(string path, CpioFormat cpioFormat = CpioFormat.OldAscii)
Parameters
path
string
The path of the archive to be created. If the specified file name points to an existing file, it will be overwritten.
cpioFormat
CpioFormat
Defines cpio header format.
Examples
using (FileStream source = File.Open("data.bin", FileMode.Open, FileAccess.Read))
{
using (var archive = new CpioArchive())
{
archive.CreateEntry("entry.bin", source);
archive.SaveGzipped("result.cpio.gz");
}
}
SaveLZMACompressed(Stream, CpioFormat)
Saves the archive to the stream with LZMA compression.
public void SaveLZMACompressed(Stream output, CpioFormat cpioFormat = CpioFormat.OldAscii)
Parameters
output
Stream
Destination stream.
cpioFormat
CpioFormat
Defines cpio header format.
Examples
using (FileStream result = File.OpenWrite("result.cpio.lzma"))
{
using (FileStream source = File.Open("data.bin", FileMode.Open, FileAccess.Read))
{
using (var archive = new CpioArchive())
{
archive.CreateEntry("entry.bin", source);
archive.SaveLZMACompressed(result);
}
}
}
Remarks
output
must be writable.
SaveLZMACompressed(string, CpioFormat)
Saves the archive to the file by path with lzma compression.
public void SaveLZMACompressed(string path, CpioFormat cpioFormat = CpioFormat.OldAscii)
Parameters
path
string
The path of the archive to be created. If the specified file name points to an existing file, it will be overwritten.
cpioFormat
CpioFormat
Defines cpio header format.
Examples
using (FileStream source = File.Open("data.bin", FileMode.Open, FileAccess.Read))
{
using (var archive = new CpioArchive())
{
archive.CreateEntry("entry.bin", source);
archive.SaveLZMACompressed("result.cpio.lzma");
}
}
Remarks
Important: cpio archive is composed then compressed within this method, its content is kept internally. Beware of memory consumption.
SaveLzipped(Stream, CpioFormat)
Saves archive to the stream with lzip compression.
public void SaveLzipped(Stream output, CpioFormat cpioFormat = CpioFormat.OldAscii)
Parameters
output
Stream
Destination stream.
cpioFormat
CpioFormat
Defines cpio header format.
Examples
using (FileStream result = File.OpenWrite("result.cpio.lz"))
{
using (FileStream source = File.Open("data.bin", FileMode.Open, FileAccess.Read))
{
using (var archive = new CpioArchive())
{
archive.CreateEntry("entry.bin", source);
archive.SaveGzipped(result);
}
}
}
Remarks
output
must be writable.
Exceptions
output
is null.
output
is not writable.
SaveLzipped(string, CpioFormat)
Saves archive to the file by path with lzip compression.
public void SaveLzipped(string path, CpioFormat cpioFormat = CpioFormat.OldAscii)
Parameters
path
string
The path of the archive to be created. If the specified file name points to an existing file, it will be overwritten.
cpioFormat
CpioFormat
Defines cpio header format.
Examples
using (FileStream source = File.Open("data.bin", FileMode.Open, FileAccess.Read))
{
using (var archive = new CpioArchive())
{
archive.CreateEntry("entry.bin", source);
archive.SaveGzipped("result.cpio.lz");
}
}
SaveXzCompressed(Stream, CpioFormat, XzArchiveSettings)
Saves archive to the stream with xz compression.
public void SaveXzCompressed(Stream output, CpioFormat cpioFormat = CpioFormat.OldAscii, XzArchiveSettings settings = null)
Parameters
output
Stream
Destination stream.
cpioFormat
CpioFormat
Defines cpio header format.
settings
XzArchiveSettings
Set of setting particular xz archive: dictionary size, block size, check type.
Examples
using (FileStream result = File.OpenWrite("result.cpio.xz"))
{
using (FileStream source = File.Open("data.bin", FileMode.Open, FileAccess.Read))
{
using (var archive = new CpioArchive())
{
archive.CreateEntry("entry.bin", source);
archive.SaveXzCompressed(result);
}
}
}
Remarks
output
The stream must be writable.
Exceptions
output
is null.
output
is not writable.
SaveXzCompressed(string, CpioFormat, XzArchiveSettings)
Saves archive to the path by path with xz compression.
public void SaveXzCompressed(string path, CpioFormat cpioFormat = CpioFormat.OldAscii, XzArchiveSettings settings = null)
Parameters
path
string
The path of the archive to be created. If the specified file name points to an existing file, it will be overwritten.
cpioFormat
CpioFormat
Defines cpio header format.
settings
XzArchiveSettings
Set of setting particular xz archive: dictionary size, block size, check type.
Examples
using (FileStream source = File.Open("data.bin", FileMode.Open, FileAccess.Read))
{
using (var archive = new CpioArchive())
{
archive.CreateEntry("entry.bin", source);
archive.SaveXzCompressed("result.cpio.xz");
}
}
SaveZCompressed(Stream, CpioFormat)
Saves archive to the stream with Z compression.
public void SaveZCompressed(Stream output, CpioFormat cpioFormat = CpioFormat.OldAscii)
Parameters
output
Stream
Destination stream.
cpioFormat
CpioFormat
Defines cpio header format.
Examples
using (FileStream result = File.OpenWrite("result.cpio.Z"))
{
using (FileStream source = File.Open("data.bin", FileMode.Open, FileAccess.Read))
{
using (var archive = new CpioArchive())
{
archive.CreateEntry("entry.bin", source);
archive.SaveZCompressed(result);
}
}
}
Remarks
output
must be writable.
Exceptions
output
is null.
output
is not writable.
SaveZCompressed(string, CpioFormat)
Saves archive to the path by path with Z compression.
public void SaveZCompressed(string path, CpioFormat cpioFormat = CpioFormat.OldAscii)
Parameters
path
string
The path of the archive to be created. If the specified file name points to an existing file, it will be overwritten.
cpioFormat
CpioFormat
Defines cpio header format.
Examples
using (FileStream source = File.Open("data.bin", FileMode.Open, FileAccess.Read))
{
using (var archive = new CpioArchive())
{
archive.CreateEntry("entry.bin", source);
archive.SaveZCompressed("result.cpio.Z");
}
}
SaveZstandard(Stream, CpioFormat)
Saves archive to the stream with Zstandard compression.
public void SaveZstandard(Stream output, CpioFormat cpioFormat = CpioFormat.OldAscii)
Parameters
output
Stream
Destination stream.
cpioFormat
CpioFormat
Defines cpio header format.
Examples
using (FileStream result = File.OpenWrite("result.cpio.zst"))
{
using (FileStream source = File.Open("data.bin", FileMode.Open, FileAccess.Read))
{
using (var archive = new CpioArchive())
{
archive.CreateEntry("entry.bin", source);
archive.SaveZstandard(result);
}
}
}
Remarks
output
must be writable.
Exceptions
output
is null.
output
is not writable.
SaveZstandard(string, CpioFormat)
Saves archive to the file by path with Zstandard compression.
public void SaveZstandard(string path, CpioFormat cpioFormat = CpioFormat.OldAscii)
Parameters
path
string
The path of the archive to be created. If the specified file name points to an existing file, it will be overwritten.
cpioFormat
CpioFormat
Defines cpio header format.
Examples
using (FileStream source = File.Open("data.bin", FileMode.Open, FileAccess.Read))
{
using (var archive = new CpioArchive())
{
archive.CreateEntry("entry.bin", source);
archive.SaveZstandard("result.cpio.zst");
}
}