-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not read position when tar archive stream is unseekable (#70178)
* src: Do not set length nor read position when stream is unseekable. * tests: Verify can write and read tar.gz archives. * tests: Add TarWriter test to write to generic unseekable stream.
- Loading branch information
1 parent
45589f2
commit 1d6f23a
Showing
4 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/libraries/System.Formats.Tar/tests/CompressedTar.Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.IO.Compression; | ||
using System.IO; | ||
using Xunit; | ||
|
||
namespace System.Formats.Tar.Tests | ||
{ | ||
public class CompressedTar_Tests : TarTestsBase | ||
{ | ||
[Fact] | ||
public void TarGz_TarWriter_TarReader() | ||
{ | ||
using TempDirectory root = new TempDirectory(); | ||
|
||
string archivePath = Path.Join(root.Path, "compressed.tar.gz"); | ||
|
||
string fileName = "file.txt"; | ||
string filePath = Path.Join(root.Path, fileName); | ||
File.Create(filePath).Dispose(); | ||
|
||
// Create tar.gz archive | ||
using (FileStream streamToCompress = File.Create(archivePath)) | ||
{ | ||
using GZipStream compressorStream = new GZipStream(streamToCompress, CompressionMode.Compress); | ||
using TarWriter writer = new TarWriter(compressorStream); | ||
writer.WriteEntry(fileName: filePath, entryName: fileName); | ||
} | ||
FileInfo fileInfo = new FileInfo(archivePath); | ||
Assert.True(fileInfo.Exists); | ||
Assert.True(fileInfo.Length > 0); | ||
|
||
// Verify tar.gz archive contents | ||
using (FileStream streamToDecompress = File.OpenRead(archivePath)) | ||
{ | ||
using GZipStream decompressorStream = new GZipStream(streamToDecompress, CompressionMode.Decompress); | ||
using TarReader reader = new TarReader(decompressorStream); | ||
TarEntry entry = reader.GetNextEntry(); | ||
Assert.Equal(TarFormat.Pax, reader.Format); | ||
Assert.Equal(fileName, entry.Name); | ||
Assert.Null(reader.GetNextEntry()); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void TarGz_TarFile_CreateFromDir_ExtractToDir() | ||
{ | ||
using TempDirectory root = new TempDirectory(); | ||
|
||
string archivePath = Path.Join(root.Path, "compressed.tar.gz"); | ||
|
||
string sourceDirectory = Path.Join(root.Path, "source"); | ||
Directory.CreateDirectory(sourceDirectory); | ||
|
||
string destinationDirectory = Path.Join(root.Path, "destination"); | ||
Directory.CreateDirectory(destinationDirectory); | ||
|
||
string fileName = "file.txt"; | ||
string filePath = Path.Join(sourceDirectory, fileName); | ||
File.Create(filePath).Dispose(); | ||
|
||
using (FileStream streamToCompress = File.Create(archivePath)) | ||
{ | ||
using GZipStream compressorStream = new GZipStream(streamToCompress, CompressionMode.Compress); | ||
TarFile.CreateFromDirectory(sourceDirectory, compressorStream, includeBaseDirectory: false); | ||
} | ||
FileInfo fileInfo = new FileInfo(archivePath); | ||
Assert.True(fileInfo.Exists); | ||
Assert.True(fileInfo.Length > 0); | ||
|
||
using (FileStream streamToDecompress = File.OpenRead(archivePath)) | ||
{ | ||
using GZipStream decompressorStream = new GZipStream(streamToDecompress, CompressionMode.Decompress); | ||
TarFile.ExtractToDirectory(decompressorStream, destinationDirectory, overwriteFiles: true); | ||
Assert.True(File.Exists(filePath)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters