Read() public method

Read the data from the stream into the buffer.

The data for the zipentry will be decrypted and uncompressed, as necessary, before being copied into the buffer.

You must set the Password property before calling Read() the first time for an encrypted entry. To determine if an entry is encrypted and requires a password, check the ZipEntry.Encryption property.

public Read ( byte buffer, int offset, int count ) : int
buffer byte The buffer to hold the data read from the stream.
offset int the offset within the buffer to copy the first byte read.
count int the number of bytes to read.
return int
示例#1
0
		public int Decompress(PointCloudTile tile, byte[] compressedBuffer, int count, byte[] uncompressedBuffer)
		{
			MemoryStream compressedStream = new MemoryStream(compressedBuffer, 0, count, false);

			int uncompressedBytes = 0;
			using (ZipInputStream zipStream = new ZipInputStream(compressedStream, true))
			{
				zipStream.GetNextEntry();
				uncompressedBytes = zipStream.Read(uncompressedBuffer, 0, uncompressedBuffer.Length);
			}

			return uncompressedBytes;
		}
示例#2
0
 // Methods
 public void Dissect()
 {
     byte[] buffer = new byte[0x800];
     using (ZipInputStream stream2 = new ZipInputStream(this.MS))
     {
         for (ZipEntry entry = stream2.GetNextEntry(); entry != null; entry = stream2.GetNextEntry())
         {
             if (!entry.IsDirectory)
             {
                 ZipFileEntry item = new ZipFileEntry
                 {
                     Name = entry.FileName
                 };
                 MemoryStream stream = new MemoryStream();
                 if (Strings.Len(this.Pass) > 0)
                 {
                     stream2.Password = this.Pass;
                 }
                 if (entry.UncompressedSize > 0L)
                 {
                     for (int i = stream2.Read(buffer, 0, buffer.Length); i > 0; i = stream2.Read(buffer, 0, buffer.Length))
                     {
                         stream.Write(buffer, 0, i);
                     }
                 }
                 item.contents = stream.ToArray();
                 this.Files.Add(item, null, null, null);
             }
             else
             {
                 ZipFileEntry entry3 = new ZipFileEntry
                 {
                     IsDir = true,
                     Name = entry.FileName
                 };
             }
         }
     }
 }
        public void ReadCompressedZip(string filepath)
        {
            while (BackgroundWriting) ;

            ZipInputStream instream = new ZipInputStream(filepath);
            BinaryFormatter formatter = new BinaryFormatter();
            instream.GetNextEntry();
            searchDump = new TCPTCPGecko.Dump((uint)formatter.Deserialize(instream), (uint)formatter.Deserialize(instream));
            instream.Read(searchDump.mem, 0, (int)(searchDump.EndAddress - searchDump.StartAddress));

            instream.GetNextEntry();
            resultsList = (System.Collections.Generic.List<UInt32>)formatter.Deserialize(instream);

            instream.Close();
            instream.Dispose();
        }
 private static byte[] ExtractLogoBin(string zipfilename)
 {
     byte[] buffer = null;
     using (var input = new ZipInputStream(zipfilename))
     {
         ZipEntry e;
         while ((e = input.GetNextEntry()) != null)
         {
             if (e.IsDirectory) continue;
             if (Path.GetFileName(e.FileName) != "logo.bin") continue;
             buffer = new byte[e.UncompressedSize];
             input.Read(buffer, 0, buffer.Length);
             break;
         }
     }
     return buffer;
 }
示例#5
0
 /// <summary>
 /// Unzips a stream and calls an action for each unzipped file.
 /// </summary>
 /// <param name="zip">The zip byte array.</param>
 /// <param name="unzipAction">The action to perform with each unzipped file.</param>
 public static void Unzip(Stream zip, Action<Path, byte[]> unzipAction)
 {
     using (var zipStream = new ZipInputStream(zip)) {
         ZipEntry theEntry;
         while ((theEntry = zipStream.GetNextEntry()) != null) {
             if (!theEntry.IsDirectory && theEntry.FileName != "") {
                 using (var streamWriter = new MemoryStream()) {
                     var data = new byte[2048];
                     while (true) {
                         var size = zipStream.Read(data, 0, data.Length);
                         if (size > 0)
                             streamWriter.Write(data, 0, size);
                         else
                             break;
                     }
                     streamWriter.Close();
                     unzipAction(new Path(theEntry.FileName), streamWriter.ToArray());
                 }
             }
         }
     }
 }
        public Dump LoadSearchDump(string filepath)
        {
            // spin while background writing to prevent us from reading a file that has yet to be written
            while (BackgroundWriting) ;

            ZipInputStream instream = new ZipInputStream(filepath);
            BinaryFormatter formatter = new BinaryFormatter();

            // First entry is the dump
            instream.GetNextEntry();
            Dump searchDump = new Dump((uint)formatter.Deserialize(instream), (uint)formatter.Deserialize(instream));
            instream.Read(searchDump.mem, 0, (int)(searchDump.EndAddress - searchDump.StartAddress));

            instream.Close();
            instream.Dispose();

            return searchDump;
        }
示例#7
0
        public void UnpackFiles(Stream strm, Callbacks callbacks)
        {
            ZipEntry ze;
            ZipInputStream zstrm = new ZipInputStream(strm, true);
            List<FileEntry> results = new List<FileEntry>();

            SetupZIPParams();
            while (true)
            {
                ze = zstrm.GetNextEntry();
                if (ze == null)
                    break;

                if (ze.IsDirectory)
                    continue;

                byte[] data = new byte[ze.UncompressedSize];
                zstrm.Read(data, 0, (int)ze.UncompressedSize);
                callbacks.WriteData(ze.FileName, data);
            };
        }
示例#8
0
        public void _Internal_Streams_WinZip_Zip(int fodderOption, string password, string label)
        {
            if (!WinZipIsPresent)
                throw new Exception("skipping test [_Internal_Streams_WinZip_Zip] : winzip is not present");

            int[] fileCounts = { 1, 2, _rnd.Next(8) + 6, _rnd.Next(18) + 16, _rnd.Next(48) + 56 };

            for (int m = 0; m < fileCounts.Length; m++)
            {
                string dirToZip = String.Format("trial{0:D2}", m);
                if (!Directory.Exists(dirToZip)) Directory.CreateDirectory(dirToZip);

                int fileCount = fileCounts[m];
                string zipFileToCreate = Path.Combine(TopLevelDir,
                                                      String.Format("Streams_Winzip_Zip.{0}.{1}.{2}.zip", fodderOption, label, m));

                string[] files = null;
                if (fodderOption == 0)
                {
                    // zero length files
                    files = new string[fileCount];
                    for (int i = 0; i < fileCount; i++)
                        files[i] = CreateZeroLengthFile(i, dirToZip);
                }
                else if (fodderOption == 1)
                    files = TestUtilities.GenerateFilesFlat(dirToZip, fileCount, 100, 72000);
                else
                {
                    // mixed
                    files = new string[fileCount];
                    for (int i = 0; i < fileCount; i++)
                    {
                        if (_rnd.Next(3) == 0)
                            files[i] = CreateZeroLengthFile(i, dirToZip);
                        else
                        {
                            files[i] = Path.Combine(dirToZip, String.Format("nonzero{0:D4}.txt", i));
                            TestUtilities.CreateAndFillFileText(files[i], _rnd.Next(60000) + 100);
                        }
                    }
                }

                // Create the zip archive via WinZip.exe
                string pwdOption = String.IsNullOrEmpty(password) ? "" : "-s" + password;
                string formatString = "-a -p {0} -yx \"{1}\" \"{2}\\*.*\"";
                string wzzipOut = this.Exec(wzzip, String.Format(formatString, pwdOption, zipFileToCreate, dirToZip));

                // Verify the number of files in the zip
                Assert.AreEqual<int>(TestUtilities.CountEntries(zipFileToCreate), files.Length,
                                     "Incorrect number of entries in the zip file.");

                // extract the files
                string extractDir = String.Format("extract{0:D2}", m);
                Directory.CreateDirectory(extractDir);
                byte[] buffer = new byte[2048];
                int n;

                using (var raw = File.OpenRead(zipFileToCreate))
                {
                    using (var input = new ZipInputStream(raw))
                    {
                        input.Password = password;
                        ZipEntry e;
                        while ((e = input.GetNextEntry()) != null)
                        {
                            TestContext.WriteLine("entry: {0}", e.FileName);
                            string outputPath = Path.Combine(extractDir, e.FileName);
                            if (e.IsDirectory)
                            {
                                // create the directory
                                Directory.CreateDirectory(outputPath);
                                continue;
                            }
                            // create the file
                            using (var output = File.Create(outputPath))
                            {
                                while ((n = input.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    output.Write(buffer, 0, n);
                                }
                            }

                            // we don't set the timestamps or attributes
                            // on the file/directory.
                        }
                    }
                }

                string[] filesUnzipped = Directory.GetFiles(extractDir);

                // Verify the number of files extracted
                Assert.AreEqual<int>(files.Length, filesUnzipped.Length,
                                     "Incorrect number of files extracted.");
            }
        }
示例#9
0
        public void _Internal_Streams_7z_Zip(int flavor, string label)
        {
            if (!SevenZipIsPresent)
            {
                TestContext.WriteLine("skipping test [_Internal_Streams_7z_Zip] : SevenZip is not present");
                return;
            }

            int[] fileCounts = { 1, 2, _rnd.Next(8) + 6, _rnd.Next(18) + 16, _rnd.Next(48) + 56 };

            for (int m = 0; m < fileCounts.Length; m++)
            {
                string dirToZip = String.Format("trial{0:D2}", m);
                if (!Directory.Exists(dirToZip)) Directory.CreateDirectory(dirToZip);

                int fileCount = fileCounts[m];
                string zipFileToCreate = Path.Combine(TopLevelDir,
                                                      String.Format("Streams_7z_Zip.{0}.{1}.{2}.zip", flavor, label, m));

                string[] files = null;
                if (flavor == 0)
                {
                    // zero length files
                    files = new string[fileCount];
                    for (int i = 0; i < fileCount; i++)
                        files[i] = CreateZeroLengthFile(i, dirToZip);
                }
                else if (flavor == 1)
                    files = TestUtilities.GenerateFilesFlat(dirToZip, fileCount, 100, 72000);
                else
                {
                    // mixed
                    files = new string[fileCount];
                    for (int i = 0; i < fileCount; i++)
                    {
                        if (_rnd.Next(3) == 0)
                            files[i] = CreateZeroLengthFile(i, dirToZip);
                        else
                        {
                            files[i] = Path.Combine(dirToZip, String.Format("nonzero{0:D4}.txt", i));
                            TestUtilities.CreateAndFillFileText(files[i], _rnd.Next(60000) + 100);
                        }
                    }
                }

                // Create the zip archive via 7z.exe
                this.Exec(sevenZip, String.Format("a \"{0}\" \"{1}\"", zipFileToCreate, dirToZip));

                // Verify the number of files in the zip
                Assert.AreEqual<int>(TestUtilities.CountEntries(zipFileToCreate), files.Length,
                                     "Incorrect number of entries in the zip file.");

                // extract the files
                string extractDir = String.Format("extract{0:D2}", m);
                byte[] buffer = new byte[2048];
                int n;
                using (var raw = File.OpenRead(zipFileToCreate))
                {
                    using (var input = new ZipInputStream(raw))
                    {
                        ZipEntry e;
                        while ((e = input.GetNextEntry()) != null)
                        {
                            TestContext.WriteLine("entry: {0}", e.FileName);
                            string outputPath = Path.Combine(extractDir, e.FileName);
                            if (e.IsDirectory)
                            {
                                // create the directory
                                Directory.CreateDirectory(outputPath);
                            }
                            else
                            {
                                // create the file
                                using (var output = File.Create(outputPath))
                                {
                                    while ((n = input.Read(buffer, 0, buffer.Length)) > 0)
                                    {
                                        output.Write(buffer, 0, n);
                                    }
                                }
                            }

                            // we don't set the timestamps or attributes
                            // on the file/directory.
                        }
                    }
                }

                // winzip does not include the base path in the filename;
                // 7zip does.
                string[] filesUnzipped = Directory.GetFiles(Path.Combine(extractDir, dirToZip));

                // Verify the number of files extracted
                Assert.AreEqual<int>(files.Length, filesUnzipped.Length,
                                     "Incorrect number of files extracted.");
            }
        }
示例#10
0
        /// <summary>
        /// Downloads the data for the specified show and returns the path to the file.
        /// </summary>
        /// <param name="show">
        /// The show to download data for. 
        /// </param>
        /// <returns>
        /// The path to the downloaded file. 
        /// </returns>
        public static string DownloadShowEpisodes(TvShow show)
        {
            for (int i = 0; i < RetryCount; i++)
            {
                try
                {
                    var webClient = new WebClient();
                    string showAddress = Mirror + ApiLoc + "/series/" + show.TvdbId + "/all/en.zip";
                    string savePath = Tvdb.CacheDirectory + show.TvdbId + ".xml";

                    if (!Directory.Exists(Tvdb.CacheDirectory))
                    {
                        Directory.CreateDirectory(Tvdb.CacheDirectory);
                    }

                    if (File.Exists(savePath))
                    {
                        File.Delete(savePath);
                    }

                    webClient.DownloadFile(showAddress, savePath + ".zip");

                    // Download the zip file to a zip stream
                    using (var stream = new ZipInputStream(savePath + ".zip"))
                    {
                        ZipEntry e;
                        var buffer = new byte[2048];

                        // Loop through all the entries looking for en.xml.
                        while ((e = stream.GetNextEntry()) != null)
                        {
                            if (!e.FileName.Equals("en.xml"))
                            {
                                while (stream.Read(buffer, 0, buffer.Length) > 0)
                                {
                                }
                            }

                            // Write the file to savePath.
                            using (var output = File.Open(savePath, FileMode.Create, FileAccess.ReadWrite))
                            {
                                int n;
                                while ((n = stream.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    output.Write(buffer, 0, n);
                                }
                            }

                            break;
                        }
                    }

                    return savePath;
                }
                catch (WebException)
                {
                    // Suppress any exceptions so the download can be retried.
                }
            }

            throw new Exception("Unable to download show data for " + show.Name);
        }
示例#11
0
        internal ZipPackage(Stream stream)
        {
            bool hasContentTypeXml = false;
            if (stream == null || stream.Length == 0)
            {
                AddNew();
            }
            else
            {
                var rels = new Dictionary<string, string>();
                stream.Seek(0, SeekOrigin.Begin);                
                using (ZipInputStream zip = new ZipInputStream(stream))
                {
                    var e = zip.GetNextEntry();
                    while (e != null)
                    {
                        if (e.UncompressedSize > 0)
                        {
                            var b = new byte[e.UncompressedSize];
                            var size = zip.Read(b, 0, (int)e.UncompressedSize);
                            if (e.FileName.Equals("[content_types].xml", StringComparison.InvariantCultureIgnoreCase))
                            {
                                AddContentTypes(Encoding.UTF8.GetString(b));
                                hasContentTypeXml = true;
                            }
                            else if (e.FileName.Equals("_rels/.rels", StringComparison.InvariantCultureIgnoreCase)) 
                            {
                                ReadRelation(Encoding.UTF8.GetString(b), "");
                            }
                            else
                            {
                                if (e.FileName.EndsWith(".rels", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    rels.Add(GetUriKey(e.FileName), Encoding.UTF8.GetString(b));
                                }
                                else
                                {
                                    var part = new ZipPackagePart(this, e);
                                    part.Stream = new MemoryStream();
                                    part.Stream.Write(b, 0, b.Length);
                                    Parts.Add(GetUriKey(e.FileName), part);
                                }
                            }
                        }
                        else
                        {
                        }
                        e = zip.GetNextEntry();
                    }

                    foreach (var p in Parts)
                    {
                        string name = Path.GetFileName(p.Key);
                        string extension = Path.GetExtension(p.Key);
                        string relFile = string.Format("{0}_rels/{1}.rels", p.Key.Substring(0, p.Key.Length - name.Length), name);
                        if (rels.ContainsKey(relFile))
                        {
                            p.Value.ReadRelation(rels[relFile], p.Value.Uri.OriginalString);
                        }
                        if (_contentTypes.ContainsKey(p.Key))
                        {
                            p.Value.ContentType = _contentTypes[p.Key].Name;
                        }
                        else if (extension.Length > 1 && _contentTypes.ContainsKey(extension.Substring(1)))
                        {
                            p.Value.ContentType = _contentTypes[extension.Substring(1)].Name;
                        }
                    }
                    if (!hasContentTypeXml)
                    {
                        throw (new InvalidDataException("The file is not an valid Package file. If the file is encrypted, please supply the password in the constructor."));
                    }
                    if (!hasContentTypeXml)
                    {
                        throw (new InvalidDataException("The file is not an valid Package file. If the file is encrypted, please supply the password in the constructor."));
                    }
                    zip.Close();
                }
            }
        }