/// <summary> /// Reads one <c>ZipEntry</c> from the given stream. The content for /// the entry does not get decompressed or decrypted. This method /// basically reads metadata, and seeks. /// </summary> /// <param name="zc">the ZipContainer this entry belongs to.</param> /// <param name="first"> /// true of this is the first entry being read from the stream. /// </param> /// <returns>the <c>ZipEntry</c> read from the stream.</returns> internal static ZipEntry ReadEntry(ZipContainer zc, bool first) { Stream s = zc.ReadStream; System.Text.Encoding defaultEncoding = zc.AlternateEncoding; ZipEntry entry = new ZipEntry(); entry._Source = ZipEntrySource.ZipFile; entry._container = zc; entry._archiveStream = s; if (first) HandlePK00Prefix(s); // Read entry header, including any encryption header if (!ReadHeader(entry, defaultEncoding)) return null; // Store the position in the stream for this entry // change for workitem 8098 entry.__FileDataPosition = entry.ArchiveStream.Position; // seek past the data without reading it. We will read on Extract() s.Seek(entry._CompressedFileDataSize + entry._LengthOfTrailer, SeekOrigin.Current); // ReadHeader moves the file pointer to the end of the entry header, // as well as any encryption header. // CompressedFileDataSize includes: // the maybe compressed, maybe encrypted file data // the encryption trailer, if any // the bit 3 descriptor, if any // workitem 5306 // http://www.codeplex.com/DotNetZip/WorkItem/View.aspx?WorkItemId=5306 HandleUnexpectedDataDescriptor(entry); return entry; }
// build the TOC by reading each entry in the file. private static void ReadIntoInstance_Orig(ZipFile zf) { //zf._entries = new System.Collections.Generic.List<ZipEntry>(); zf._entries = new System.Collections.Generic.Dictionary <String, ZipEntry>(); ZipEntry e; // work item 6647: PK00 (packed to removable disk) bool firstEntry = true; ZipContainer zc = new ZipContainer(zf); while ((e = ZipEntry.ReadEntry(zc, firstEntry)) != null) { zf._entries.Add(e.FileName, e); firstEntry = false; } // read the zipfile's central directory structure here. // workitem 9912 // But, because it may be corrupted, ignore errors. try { ZipEntry de; // in lieu of hashset, use a dictionary var previouslySeen = new Dictionary <String, Object>(); while ((de = ZipEntry.ReadDirEntry(zf, previouslySeen)) != null) { // Housekeeping: Since ZipFile exposes ZipEntry elements in the enumerator, // we need to copy the comment that we grab from the ZipDirEntry // into the ZipEntry, so the application can access the comment. // Also since ZipEntry is used to Write zip files, we need to copy the // file attributes to the ZipEntry as appropriate. ZipEntry e1 = zf._entries[de.FileName]; if (e1 != null) { e1._Comment = de.Comment; if (de.IsDirectory) { e1.MarkAsDirectory(); } } previouslySeen.Add(de.FileName, null); // to prevent dupes } // workitem 8299 if (zf._locEndOfCDS > 0) { zf.ReadStream.Seek(zf._locEndOfCDS, SeekOrigin.Begin); } ReadCentralDirectoryFooter(zf); } catch (ZipException) { } catch (IOException) { } }
private void InternalExtractToStream(Stream outStream, string password, ZipContainer zipContainer, ZipEntrySource zipEntrySource, string fileName) { // workitem 7958 if (zipContainer == null) { throw new BadStateException("This entry is an orphan"); } // workitem 10355 if (zipContainer.ZipFile == null) { throw new InvalidOperationException("Use Extract() only with ZipFile."); } if (zipEntrySource != ZipEntrySource.ZipFile) { throw new BadStateException("You must call ZipFile.Save before calling any Extract method"); } _ioOperationCanceled = false; try { ValidateCompression(_CompressionMethod_FromZipFile, fileName, GetUnsupportedCompressionMethod(_CompressionMethod)); ValidateEncryption(Encryption, fileName, _UnsupportedAlgorithmId); if (IsDoneWithOutputToStream()) { return; } // If no password explicitly specified, use the password on the entry itself, // or on the zipfile itself. if (_Encryption_FromZipFile != EncryptionAlgorithm.None) { EnsurePassword(password); } if (ExtractToStream(ArchiveStream, outStream, Encryption, _Crc32)) { goto ExitTry; } ExitTry :; } catch (Exception) { _ioOperationCanceled = true; throw; } }
/// <summary> /// Reads one <c>ZipEntry</c> from the given stream. The content for /// the entry does not get decompressed or decrypted. This method /// basically reads metadata, and seeks. /// </summary> /// <param name="zc">the ZipContainer this entry belongs to.</param> /// <param name="first"> /// true of this is the first entry being read from the stream. /// </param> /// <returns>the <c>ZipEntry</c> read from the stream.</returns> internal static ZipEntry ReadEntry(ZipContainer zc, bool first) { Stream s = zc.ReadStream; System.Text.Encoding defaultEncoding = zc.AlternateEncoding; ZipEntry entry = new ZipEntry(); entry._Source = ZipEntrySource.ZipFile; entry._container = zc; entry._archiveStream = s; if (first) { HandlePK00Prefix(s); } // Read entry header, including any encryption header if (!ReadHeader(entry, defaultEncoding)) { return(null); } // Store the position in the stream for this entry // change for workitem 8098 entry.__FileDataPosition = entry.ArchiveStream.Position; // seek past the data without reading it. We will read on Extract() s.Seek(entry._CompressedFileDataSize + entry._LengthOfTrailer, SeekOrigin.Current); // ReadHeader moves the file pointer to the end of the entry header, // as well as any encryption header. // CompressedFileDataSize includes: // the maybe compressed, maybe encrypted file data // the encryption trailer, if any // the bit 3 descriptor, if any // workitem 5306 // http://www.codeplex.com/DotNetZip/WorkItem/View.aspx?WorkItemId=5306 HandleUnexpectedDataDescriptor(entry); return(entry); }
private void InternalExtractToStream(Stream outStream, string password, ZipContainer zipContainer, ZipEntrySource zipEntrySource, string fileName) { // workitem 7958 if (zipContainer == null) throw new BadStateException("This entry is an orphan"); // workitem 10355 if (zipContainer.ZipFile == null) throw new InvalidOperationException("Use Extract() only with ZipFile."); if (zipEntrySource != ZipEntrySource.ZipFile) throw new BadStateException("You must call ZipFile.Save before calling any Extract method"); _ioOperationCanceled = false; try { ValidateCompression(_CompressionMethod_FromZipFile, fileName, GetUnsupportedCompressionMethod(_CompressionMethod)); ValidateEncryption(Encryption, fileName, _UnsupportedAlgorithmId); if (IsDoneWithOutputToStream()) { return; } // If no password explicitly specified, use the password on the entry itself, // or on the zipfile itself. if (_Encryption_FromZipFile != EncryptionAlgorithm.None) EnsurePassword(password); if (ExtractToStream(ArchiveStream, outStream, Encryption, _Crc32)) goto ExitTry; ExitTry: ; } catch (Exception) { _ioOperationCanceled = true; throw; } }
// build the TOC by reading each entry in the file. private static void ReadIntoInstance_Orig(ZipFile zf) { //zf._entries = new System.Collections.Generic.List<ZipEntry>(); zf._entries = new System.Collections.Generic.Dictionary<String,ZipEntry>(); ZipEntry e; // work item 6647: PK00 (packed to removable disk) bool firstEntry = true; ZipContainer zc = new ZipContainer(zf); while ((e = ZipEntry.ReadEntry(zc, firstEntry)) != null) { zf._entries.Add(e.FileName,e); firstEntry = false; } // read the zipfile's central directory structure here. // workitem 9912 // But, because it may be corrupted, ignore errors. try { ZipEntry de; // in lieu of hashset, use a dictionary var previouslySeen = new Dictionary<String,Object>(); while ((de = ZipEntry.ReadDirEntry(zf, previouslySeen)) != null) { // Housekeeping: Since ZipFile exposes ZipEntry elements in the enumerator, // we need to copy the comment that we grab from the ZipDirEntry // into the ZipEntry, so the application can access the comment. // Also since ZipEntry is used to Write zip files, we need to copy the // file attributes to the ZipEntry as appropriate. ZipEntry e1 = zf._entries[de.FileName]; if (e1 != null) { e1._Comment = de.Comment; if (de.IsDirectory) e1.MarkAsDirectory(); } previouslySeen.Add(de.FileName,null); // to prevent dupes } // workitem 8299 if (zf._locEndOfCDS > 0) zf.ReadStream.Seek(zf._locEndOfCDS, SeekOrigin.Begin); ReadCentralDirectoryFooter(zf); } catch (ZipException) { } catch (IOException) { } }