This topic demonstrates how to extract items from a Tar archive using the static Untar method of the QuickTar class, specifying several parameters.
Tar is not currently available in Xceed's Compact Framework products.
Untar method
The Untar method has a few overloads that can be used to extract files from a Tar archive. Some only require that you specify the archive name and the files to extract, the destination folder, and whether the Tar archive is compressed by GZip, while others provide options such as whether existing files should be replaced and the directory structure preserved in the destination Tar archive, whether the Tar operation should be performed recursively. Events are also supported. For details on the other overloads, see the reference documentation.
Demonstration
In the following example, we specify several parameters and also use some callbacks.<
VB.NET | Copy Code |
---|---|
|
C# | Copy Code |
---|---|
|
Handling paths
When extracting files from within a Tar archive file, the directory structure can be restored fully or partrially, or it can be omitted altogether.
If the preservePaths parameter of the Untar method is set to false, the files specified in the filesToUntar parameter will be restored directly into the root of the destination folder without recreating the directory structure.
For example, if you have a Tar archive containing files in the "folder1" subfolder and files in the "folder1\folder2" subfolder, the following code will untar all files right into "c:\temp", without creating any subfolders:
VB.NET | Copy Code |
---|---|
QuickTar.Untar( "c:\test.tar", "c:\temp", True, True, False, "folder1\*" ) |
C# | Copy Code |
---|---|
QuickTar.Untar( @"c:\test.tar", @"c:\temp", true, true, false, @"folder1\*" ); |
If however the preservePaths parameter is set to true, the part of the path that is explicitly included in the filesToUntar parameter will not be restored into the destination folder.
For example, for the same Tar archive as above, the following code will create the folder "folder2" into the "c:\temp" destination folder. Files that were in the "folder1" subfolder in the archive will be extracted directly into the root of "c:\temp", and files that were in "folder1\folder2" will be extracted into "c:\temp\folder2":
VB.NET | Copy Code |
---|---|
QuickTar.Untar( "c:\test.tar", "c:\temp", True, True, True, "folder1\*" ) |
C# | Copy Code |
---|---|
QuickTar.Untar( @"c:\test.tar", @"c:\temp", true, true, true, @"folder1\*" ); |
The following example will extract files into "c:\temp\folder1" and "c:\temp\folder1\folder2":
VB.NET | Copy Code |
---|---|
QuickTar.Untar( "c:\test.tar", "c:\temp", True, True, True, "*" ) |
C# | Copy Code |
---|---|
QuickTar.Untar( @"c:\test.tar", @"c:\temp", true, true, true, @"*" ); |
Remarks
When recursively adding files to a zip file, you have to consider every filename you place in the filesToTar parameter as a filemask. For example, if you set the filesToTar parameter to "c:\file.txt", the entire "c:\" drive will be scanned and all the files that are named "file.txt" that are found will be included in the Tar archive.
Note that the filesToTar parameter of the Tar method cannot be null; otherwise, an ArgumentNullException exception will be thrown.
If the isTarCompressed parameter is set to true, the GZip file retreived must have the following format: tarFileName.gz. An exception is thrown if the Tar archive is not compressed.
Things you should consider
The main questions you should ask yourself when adding items to a Tar archive are:
-
Do you want to do more complex Tar operations? Use the FileSystem-based classes defined within the Xceed.Tar namespace.