@@ -859,7 +859,7 @@ public void ParseHeaderWithEncoding(int length, string encodingName)
859
859
reparseHeader . ParseBuffer ( headerbytes , enc ) ;
860
860
Assert . AreEqual ( name , reparseHeader . Name ) ;
861
861
// top 100 bytes are name field in tar header
862
- for ( int i = 0 ; i < encodedName . Length ; i ++ )
862
+ for ( int i = 0 ; i < encodedName . Length ; i ++ )
863
863
{
864
864
Assert . AreEqual ( encodedName [ i ] , headerbytes [ i ] ) ;
865
865
}
@@ -878,17 +878,17 @@ public void StreamWithJapaneseName(int length, string encodingName)
878
878
var entryName = new string ( ( char ) 0x3042 , length ) ;
879
879
var data = new byte [ 32 ] ;
880
880
var encoding = Encoding . GetEncoding ( encodingName ) ;
881
- using ( var memoryStream = new MemoryStream ( ) )
881
+ using ( var memoryStream = new MemoryStream ( ) )
882
882
{
883
- using ( var tarOutput = new TarOutputStream ( memoryStream , encoding ) )
883
+ using ( var tarOutput = new TarOutputStream ( memoryStream , encoding ) )
884
884
{
885
885
var entry = TarEntry . CreateTarEntry ( entryName ) ;
886
886
entry . Size = 32 ;
887
887
tarOutput . PutNextEntry ( entry ) ;
888
888
tarOutput . Write ( data , 0 , data . Length ) ;
889
889
}
890
- using ( var memInput = new MemoryStream ( memoryStream . ToArray ( ) ) )
891
- using ( var inputStream = new TarInputStream ( memInput , encoding ) )
890
+ using ( var memInput = new MemoryStream ( memoryStream . ToArray ( ) ) )
891
+ using ( var inputStream = new TarInputStream ( memInput , encoding ) )
892
892
{
893
893
var buf = new byte [ 64 ] ;
894
894
var entry = inputStream . GetNextEntry ( ) ;
@@ -899,5 +899,72 @@ public void StreamWithJapaneseName(int length, string encodingName)
899
899
File . WriteAllBytes ( Path . Combine ( Path . GetTempPath ( ) , $ "jpnametest_{ length } _{ encodingName } .tar") , memoryStream . ToArray ( ) ) ;
900
900
}
901
901
}
902
+ [ Test ]
903
+ [ Category ( "Tar" ) ]
904
+ public void rootPathIsRespected ( )
905
+ {
906
+ // create dummy folder structure
907
+ var tempDirectory = Path . Combine ( Path . GetTempPath ( ) , "sharpziplib_tar_test_folder" ) ;
908
+ CreateAndClearDirectory ( tempDirectory ) ;
909
+ using ( var dummyfile = File . Create ( "dummyfile" ) )
910
+ {
911
+ using ( var randomStream = new ChaosStream ( ) )
912
+ {
913
+ randomStream . CopyTo ( dummyfile ) ;
914
+ }
915
+ }
916
+
917
+ var tarFileName = Path . Combine ( Path . GetTempPath ( ) , "sharpziplib_tar_test_folder_archive.tar" ) ;
918
+
919
+ using ( var tarFile = File . Open ( tarFileName , FileMode . Create ) )
920
+ {
921
+ using ( var tarOutputStream = TarArchive . CreateOutputTarArchive ( tarFile ) )
922
+ {
923
+ tarOutputStream . RootPath = tempDirectory ;
924
+ var entry = TarEntry . CreateEntryFromFile ( tempDirectory ) ;
925
+ tarOutputStream . WriteEntry ( entry , true ) ;
926
+ }
927
+ }
928
+
929
+ var extractDirectory = Path . Combine ( Path . GetTempPath ( ) , "sharpziplib_tar_extract_folder" ) ;
930
+ CreateAndClearDirectory ( extractDirectory ) ;
931
+ using ( var file = File . OpenRead ( tarFileName ) )
932
+ {
933
+ using ( var archive = TarArchive . CreateInputTarArchive ( file , Encoding . UTF8 ) )
934
+ {
935
+ archive . ExtractContents ( extractDirectory ) ;
936
+ }
937
+ }
938
+
939
+ var expectationDirectory = new DirectoryInfo ( tempDirectory ) ;
940
+ foreach ( var checkFile in expectationDirectory . GetFiles ( "" , SearchOption . AllDirectories ) )
941
+ {
942
+ var relativePath = expectationDirectory . FullName . Substring ( expectationDirectory . FullName . Length ) ;
943
+ FileAssert . Exists ( Path . Combine ( extractDirectory , relativePath ) ) ;
944
+ FileAssert . AreEqual ( checkFile . FullName , Path . Combine ( extractDirectory , relativePath ) ) ;
945
+ }
946
+ }
947
+
948
+ private void CreateAndClearDirectory ( string path )
949
+ {
950
+ if ( Directory . Exists ( path ) )
951
+ {
952
+ Directory . Delete ( path ) ;
953
+ }
954
+ Directory . CreateDirectory ( path ) ;
955
+ }
956
+
957
+ public class ChaosStream : MemoryStream
958
+ {
959
+ private readonly int length = new Random ( ) . Next ( ) % 5000 + 200 ;
960
+
961
+ // Create constructors as needed to match desired MemoryStream construction
962
+
963
+ public override int Read ( byte [ ] buffer , int offset , int count )
964
+ {
965
+ int readCount = Math . Max ( 0 , Math . Min ( length - offset , count ) ) ;
966
+ return base . Read ( buffer , offset , readCount ) ;
967
+ }
968
+ }
902
969
}
903
970
}
0 commit comments