示例#1
0
 private void TestReadData(AlignedReadStream readStream, byte[] data, int offset, int count)
 {
     var buffer = new byte[count];
     readStream.Seek(offset, SeekOrigin.Begin);
     readStream.Read(buffer, 0, count);
     Assert.IsTrue(buffer.SequenceEqual(data.Skip(offset).Take(count)), "Data mismatch");
 }
示例#2
0
 private void TestReadStreamRandomOperations(int count, string inputfile, int bufferSize)
 {
     byte[] data = File.ReadAllBytes(inputfile);
     using (MemoryStream dataStream = new MemoryStream(data))
     {
         using (FileStream sourceFileStream = new FileStream(inputfile,
             FileMode.Open, FileAccess.Read, FileShare.None, bufferSize,
             Win32.FileFlagNoBuffering))
         {
             using (AlignedReadStream readStream = new AlignedReadStream(sourceFileStream, bufferSize))
             {
                 while (0 < count)
                 {
                     this.TestRandomOperation(readStream, dataStream);
                     count--;
                 }
             }
         }
     }
 }
示例#3
0
 private void TestRandomReadOperation(AlignedReadStream readStream, Stream dataStream)
 {
     int offsetUpperBound = (int)(dataStream.Length - dataStream.Position);
     int offset = Random.Next(offsetUpperBound);
     if (0 == Random.Next(2))
     {
         offset -= (offset % readStream.AlignmentSize);
     }
     int countUpperBound = (int)(dataStream.Length - dataStream.Position - offset);
     int count = Random.Next(countUpperBound);
     if (0 == Random.Next(2))
     {
         count -= (count % readStream.AlignmentSize);
     }
     this.TestRandomReadOperation(readStream, dataStream, offset, count);
 }
示例#4
0
        private void TestRandomReadOperation(AlignedReadStream readStream, Stream dataStream, int offset, int count)
        {
            var buffer1 = new byte[offset + count];
            int bytesRead1 = readStream.Read(buffer1, offset, count);
            var buffer2 = new byte[offset + count];
            int bytesRead2 = dataStream.Read(buffer2, offset, count);

            Assert.AreEqual(readStream.Position, dataStream.Position, "Position mismatch");
            Assert.AreEqual(bytesRead1, bytesRead2, "Bytes read mismatch");
            Assert.IsTrue(buffer1.SequenceEqual(buffer2), "Data mismatch");
        }
示例#5
0
 private void TestRandomReadData(int count, string inputfile, int bufferSize, Action<AlignedReadStream, byte[]> readTester)
 {
     byte[] data = File.ReadAllBytes(inputfile);
     using (FileStream sourceFileStream = new FileStream(inputfile,
         FileMode.Open, FileAccess.Read, FileShare.None, bufferSize,
         Win32.FileFlagNoBuffering))
     {
         using (AlignedReadStream readStream = new AlignedReadStream(sourceFileStream, bufferSize))
         {
             while (0 < count)
             {
                 readTester(readStream, data);
                 count--;
             }
         }
     }
 }
示例#6
0
        private void TestRandomReadDataAlignedOffset(AlignedReadStream readStream, byte[] data)
        {
            int positionUpperBound = data.Length;
            int position = Random.Next(positionUpperBound);
            position -= (position % readStream.AlignmentSize);
            int countUpperBound = data.Length - position;
            int count = Random.Next(countUpperBound);

            this.TestReadData(readStream, data, position, count);
        }
示例#7
0
 private void TestAlignedReadStreamPosition(string inputfile, int bufferSize)
 {
     var buffer = new byte[1000];
     using (FileStream sourceFileStream = new FileStream(inputfile,
         FileMode.Open, FileAccess.Read, FileShare.None, bufferSize,
         Win32.FileFlagNoBuffering | FileOptions.SequentialScan))
     {
         using (AlignedReadStream readStream = new AlignedReadStream(sourceFileStream))
         {
             readStream.Seek(12, SeekOrigin.Begin);
             Assert.AreEqual(12, readStream.Position, "Invlid position");
             Assert.AreEqual(0, sourceFileStream.Position, "Invlid position");
             readStream.Seek(512, SeekOrigin.Begin);
             Assert.AreEqual(512, readStream.Position, "Invlid position");
             Assert.AreEqual(512, sourceFileStream.Position, "Invlid position");
             readStream.Seek(600, SeekOrigin.Begin);
             Assert.AreEqual(600, readStream.Position, "Invlid position");
             Assert.AreEqual(512, sourceFileStream.Position, "Invlid position");
             readStream.Seek(4096, SeekOrigin.Begin);
             Assert.AreEqual(4096, readStream.Position, "Invlid position");
             Assert.AreEqual(4096, sourceFileStream.Position, "Invlid position");
             readStream.Seek(-3000, SeekOrigin.Current);
             Assert.AreEqual(1096, readStream.Position, "Invlid position");
             Assert.AreEqual(1024, sourceFileStream.Position, "Invlid position");
             readStream.Seek(1024, SeekOrigin.Current);
             Assert.AreEqual(2120, readStream.Position, "Invlid position");
             Assert.AreEqual(2048, sourceFileStream.Position, "Invlid position");
             readStream.Seek(3385, SeekOrigin.Current);
             Assert.AreEqual(5505, readStream.Position, "Invlid position");
             Assert.AreEqual(5120, sourceFileStream.Position, "Invlid position");
             readStream.Seek(-100, SeekOrigin.End);
             Assert.AreEqual(readStream.Length - 100, readStream.Position, "Invlid position");
             Assert.AreEqual(sourceFileStream.Length - 100 - (sourceFileStream.Length - 100) % 512, sourceFileStream.Position, "Invlid position");
             readStream.Seek(-512, SeekOrigin.End);
             Assert.AreEqual(readStream.Length - 512, readStream.Position, "Invlid position");
             Assert.AreEqual(sourceFileStream.Length - 512 - (sourceFileStream.Length - 512) % 512, sourceFileStream.Position, "Invlid position");
             readStream.Seek(-2658, SeekOrigin.End);
             Assert.AreEqual(readStream.Length - 2658, readStream.Position, "Invlid position");
             Assert.AreEqual(sourceFileStream.Length - 2658 - (sourceFileStream.Length - 2658) % 512, sourceFileStream.Position, "Invlid position");
             readStream.Seek(0, SeekOrigin.Begin);
             Assert.AreEqual(0, readStream.Position, "Invlid position");
             Assert.AreEqual(0, sourceFileStream.Position, "Invlid position");
             readStream.Read(buffer, 0, buffer.Length);
             Assert.AreEqual(buffer.Length, readStream.Position, "Invlid position");
             Assert.AreEqual(buffer.Length + (512 - buffer.Length % 512), sourceFileStream.Position, "Invlid position");
             readStream.Read(buffer, 0, 4);
             Assert.AreEqual(buffer.Length + 4, readStream.Position, "Invlid position");
             Assert.AreEqual(buffer.Length + 4 + (512 - (buffer.Length + 4) % 512), sourceFileStream.Position, "Invlid position");
             readStream.Read(buffer, 0, 21);
             Assert.AreEqual(buffer.Length + 4 + 21, readStream.Position, "Invlid position");
             Assert.AreEqual(buffer.Length + 4 + 21 + (512 - (buffer.Length + 4 + 21) % 512), sourceFileStream.Position, "Invlid position");
             readStream.Read(buffer, 0, 1);
             Assert.AreEqual(buffer.Length + 4 + 21 + 1, readStream.Position, "Invlid position");
             Assert.AreEqual(buffer.Length + 4 + 21 + 1 + (512 - (buffer.Length + 4 + 21 + 1) % 512), sourceFileStream.Position, "Invlid position");
             readStream.Read(buffer, 0, 510);
             Assert.AreEqual(buffer.Length + 4 + 21 + 1 + 510, readStream.Position, "Invlid position");
             Assert.AreEqual(readStream.Position, sourceFileStream.Position, "Invlid position");
             readStream.Read(buffer, 0, 24);
             Assert.AreEqual(buffer.Length + 4 + 21 + 1 + 510 + 24, readStream.Position, "Invlid position");
             Assert.AreEqual(buffer.Length + 4 + 21 + 1 + 510 + 24 + (512 - (buffer.Length + 4 + 21 + 1 + 510 + 24) % 512), sourceFileStream.Position, "Invlid position");
             readStream.Read(buffer, 0, buffer.Length);
             Assert.AreEqual(2 * buffer.Length + 4 + 21 + 1 + 510 + 24, readStream.Position, "Invlid position");
             Assert.AreEqual(readStream.Position, sourceFileStream.Position, "Invlid position");
             readStream.Seek(0, SeekOrigin.End);
             Assert.AreEqual(readStream.Length, readStream.Position, "Invlid position");
             Assert.AreEqual(sourceFileStream.Length - (sourceFileStream.Length % 512), sourceFileStream.Position, "Invlid position");
             readStream.Read(buffer, 0, buffer.Length);
             Assert.AreEqual(readStream.Length, readStream.Position, "Invlid position");
             Assert.AreEqual(sourceFileStream.Length, sourceFileStream.Position, "Invlid position");
             readStream.Read(buffer, 0, buffer.Length);
             Assert.AreEqual(readStream.Length, readStream.Position, "Invlid position");
             Assert.AreEqual(sourceFileStream.Length, sourceFileStream.Position, "Invlid position");
             readStream.Seek(0, SeekOrigin.Begin);
             Assert.AreEqual(0, readStream.Position, "Invlid position");
             Assert.AreEqual(0, sourceFileStream.Position, "Invlid position");
             readStream.Seek(2, SeekOrigin.Begin);
             Assert.AreEqual(2, readStream.Position, "Invlid position");
             Assert.AreEqual(0, sourceFileStream.Position, "Invlid position");
             readStream.Read(buffer, 0, 10);
             Assert.AreEqual(12, readStream.Position, "Invlid position");
             Assert.AreEqual(512, sourceFileStream.Position, "Invlid position");
             readStream.Seek(500, SeekOrigin.Begin);
             Assert.AreEqual(500, readStream.Position, "Invlid position");
             Assert.AreEqual(512, sourceFileStream.Position, "Invlid position");
             readStream.Read(buffer, 0, 12);
             Assert.AreEqual(512, readStream.Position, "Invlid position");
             Assert.AreEqual(readStream.Position, sourceFileStream.Position, "Invlid position");
             readStream.Seek(-1 * readStream.Length % 512, SeekOrigin.End);
             Assert.AreEqual(readStream.Length - readStream.Length % 512, readStream.Position, "Invlid position");
             Assert.AreEqual(readStream.Position, sourceFileStream.Position, "Invlid position");
             readStream.Read(buffer, 0, 24);
             Assert.AreEqual((readStream.Length - readStream.Length % 512) + 24, readStream.Position, "Invlid position");
             Assert.AreEqual(sourceFileStream.Length, sourceFileStream.Position, "Invlid position");
             readStream.Seek(100, SeekOrigin.Current);
             Assert.AreEqual((readStream.Length - readStream.Length % 512) + 24 + 100, readStream.Position, "Invlid position");
             Assert.AreEqual(sourceFileStream.Length, sourceFileStream.Position, "Invlid position");
             readStream.Read(buffer, 0, 100);
             Assert.AreEqual((readStream.Length - readStream.Length % 512) + 24 + 100 + 100, readStream.Position, "Invlid position");
             Assert.AreEqual(sourceFileStream.Length, sourceFileStream.Position, "Invlid position");
             readStream.Seek(0, SeekOrigin.End);
             Assert.AreEqual(readStream.Length, readStream.Position, "Invlid position");
             Assert.AreEqual(sourceFileStream.Length, sourceFileStream.Position, "Invlid position");
         }
     }
 }
示例#8
0
 private void TestRandomOperation(AlignedReadStream readStream, Stream dataStream)
 {
     ReadSreamOperations operation = (ReadSreamOperations)Random.Next((int)ReadSreamOperations.Count);
     switch (operation)
     {
         case ReadSreamOperations.Seek:
             this.TestRandomSeekOperation(readStream, dataStream, (int)dataStream.Length);
             break;
         case ReadSreamOperations.Read:
             this.TestRandomReadOperation(readStream, dataStream);
             break;
         default:
             Assert.Fail("Invalid operation");
             break;
     }
 }
示例#9
0
 protected void Decompress(FileStream sourceFileStream, string targetPath, FileMode fileMode, string password,
     int bufferSize, int preallocationPercent)
 {
     using (AlignedReadStream readArchiveStream = new AlignedReadStream(sourceFileStream, bufferSize))
     {
         SevenZipExtractor extractor = this.CreateExtractor(readArchiveStream, password);
         this.Decompress(extractor, targetPath, fileMode, bufferSize, preallocationPercent);
     }
 }
示例#10
0
 protected SevenZipExtractor CreateExtractor(AlignedReadStream readArchiveStream, string password)
 {
     if (string.IsNullOrEmpty(password))
     {
         return new SevenZipExtractor(readArchiveStream);
     }
     else
     {
         return new SevenZipExtractor(readArchiveStream, password);
     }
 }
示例#11
0
 protected long Compress(SevenZipCompressor compressor, AlignedReadStream readStream, AlignedWriteStream writeStream,
     string password)
 {
     using (MaxPositionStream maxWriteStream = new MaxPositionStream(writeStream))
     {
         if (string.IsNullOrEmpty(password))
         {
             compressor.CompressStream(readStream, maxWriteStream);
         }
         else
         {
             compressor.CompressStream(readStream, maxWriteStream, password);
         }
         return maxWriteStream.MaxPosition;
     }
 }
示例#12
0
 protected void Compress(SevenZipCompressor compressor, FileStream sourceFileStream, FileStream targetFileStream,
     string password, int preallocationPercent, bool check, int bufferSize)
 {
     using (AlignedReadStream readStream = new AlignedReadStream(sourceFileStream, bufferSize))
     {
         decimal preallocationSize = (((decimal)readStream.Length * preallocationPercent) / 100);
         preallocationSize -= preallocationSize % this._sectorSize;
         if (targetFileStream.Length < preallocationSize)
         {
             targetFileStream.SetLength((long)preallocationSize);
         }
         long compressedLength = 0;
         using (AlignedWriteStream writeStream = new AlignedWriteStream(targetFileStream, bufferSize))
         {
             compressedLength = this.Compress(compressor, readStream, writeStream, password);
         }
         if (targetFileStream.Length != compressedLength)
         {
             this.SetFileLength(targetFileStream.SafeFileHandle, compressedLength);
         }
         if (check)
         {
             this.Check(targetFileStream.SafeFileHandle, password, bufferSize);
         }
     }
 }
示例#13
0
 protected void Check(FileStream fileStream, string password, int bufferSize)
 {
     using (AlignedReadStream readArchiveStream = new AlignedReadStream(fileStream, bufferSize))
     {
         readArchiveStream.Seek(0, SeekOrigin.Begin);
         SevenZipExtractor extractor = this.CreateExtractor(readArchiveStream, password);
         if (extractor.Check())
         {
             return;
         }
         throw new InvalidArchiveException();
     }
 }