Exemplo n.º 1
0
        public void TestSharpCompressWithEmptyStream()
        {
            ResetScratch();

            MemoryStream stream = new NonSeekableMemoryStream();

            using (IWriter zipWriter = WriterFactory.Open(stream, ArchiveType.Zip, CompressionType.Deflate))
            {
                zipWriter.Write("foo.txt", new MemoryStream(new byte[0]));
                zipWriter.Write("foo2.txt", new MemoryStream(new byte[10]));
            }

            stream = new MemoryStream(stream.ToArray());
            File.WriteAllBytes(Path.Combine(SCRATCH_FILES_PATH, "foo.zip"), stream.ToArray());

            using (IReader zipReader = ZipReader.Open(stream))
            {
                while (zipReader.MoveToNextEntry())
                {
                    using (EntryStream entry = zipReader.OpenEntryStream())
                    {
                        MemoryStream tempStream = new MemoryStream();
                        const int    bufSize    = 0x1000;
                        byte[]       buf        = new byte[bufSize];
                        int          bytesRead  = 0;
                        while ((bytesRead = entry.Read(buf, 0, bufSize)) > 0)
                        {
                            tempStream.Write(buf, 0, bytesRead);
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void TestSharpCompressWithEmptyStream()
        {
            var expected = new Tuple <string, byte[]>[]
            {
                new Tuple <string, byte[]>("foo.txt", new byte[0]),
                new Tuple <string, byte[]>("foo2.txt", new byte[10])
            };

            using (var memory = new MemoryStream())
            {
                Stream stream = new TestStream(memory, read: true, write: true, seek: false);

                using (IWriter zipWriter = WriterFactory.Open(stream, ArchiveType.Zip, CompressionType.Deflate))
                {
                    zipWriter.Write(expected[0].Item1, new MemoryStream(expected[0].Item2));
                    zipWriter.Write(expected[1].Item1, new MemoryStream(expected[1].Item2));
                }

                stream = new MemoryStream(memory.ToArray());
                File.WriteAllBytes(Path.Combine(SCRATCH_FILES_PATH, "foo.zip"), memory.ToArray());

                using (IReader zipReader = ZipReader.Open(new NonDisposingStream(stream, true)))
                {
                    var i = 0;
                    while (zipReader.MoveToNextEntry())
                    {
                        using (EntryStream entry = zipReader.OpenEntryStream())
                        {
                            MemoryStream tempStream = new MemoryStream();
                            const int    bufSize    = 0x1000;
                            byte[]       buf        = new byte[bufSize];
                            int          bytesRead  = 0;
                            while ((bytesRead = entry.Read(buf, 0, bufSize)) > 0)
                            {
                                tempStream.Write(buf, 0, bytesRead);
                            }

                            Assert.Equal(expected[i].Item1, zipReader.Entry.Key);
                            Assert.Equal(expected[i].Item2, tempStream.ToArray());
                        }
                        i++;
                    }
                }
            }
        }
Exemplo n.º 3
0
 private void Skip()
 {
     if (!this.Entry.IsSolid)
     {
         Stream rawStream = Enumerable.First <FilePart>(this.Entry.Parts).GetRawStream();
         if (rawStream != null)
         {
             long compressedSize = this.Entry.CompressedSize;
             for (int i = 0; i < (compressedSize / ((long)this.skipBuffer.Length)); i++)
             {
                 rawStream.Read(this.skipBuffer, 0, this.skipBuffer.Length);
             }
             rawStream.Read(this.skipBuffer, 0, (int)(compressedSize % ((long)this.skipBuffer.Length)));
             return;
         }
     }
     using (EntryStream stream2 = this.OpenEntryStream())
     {
         while (stream2.Read(this.skipBuffer, 0, this.skipBuffer.Length) > 0)
         {
         }
     }
 }