/// <summary> /// Advances the enumerator to the next element of the collection /// </summary> /// <summary>Sets <see cref="_currentZipEntry"/> to the next zip entry</summary> /// <returns>true if the next entry is not null, otherwise false</returns> private bool MoveNext() { int result; if (_currentZipEntry == null) { result = ZlibWrapper.ZlibUnzGoToFirstFile(_zipFileHandle); } else { CloseCurrentEntry(); result = ZlibWrapper.ZlibUnzGoToNextFile(_zipFileHandle); } if (result == ZipReturnCode.EndOfListOfFile) { // No more entries _currentZipEntry = null; } else if (result < 0) { throw new ZipException("MoveNext failed.", result); } else { // Entry found,m open it OpenCurrentEntry(); } return(_currentZipEntry != null); }
/// <summary> /// Creates a new Zip file entry reading values from a zip file /// </summary> internal ZipEntry(IntPtr handle) { var entryInfo = new ZipEntryInfo64(); int result = ZlibWrapper.ZlibUnzGetCurrentFileInfo64(handle, out entryInfo, null, 0, null, 0, null, 0); if (result != 0) { throw new ZipException($"Could not read entry from zip file \"{Name}\"", result); } byte[] extraField = new byte[entryInfo.ExtraFieldLength]; byte[] entryNameBuffer = new byte[entryInfo.FileNameLength]; byte[] commentBuffer = new byte[entryInfo.CommentLength]; result = ZlibWrapper.ZlibUnzGetCurrentFileInfo64(handle, out entryInfo, entryNameBuffer, (uint)entryNameBuffer.Length, extraField, (uint)extraField.Length, commentBuffer, (uint)commentBuffer.Length); if (result != 0) { throw new ZipException($"Could not read entry from zip file \"{Name}\"", result); } _utf8Encoding = (entryInfo.Flag & ZipEntryFlag.UTF8) == ZipEntryFlag.UTF8; Encoding encoding = _utf8Encoding ? Encoding.UTF8 : ZlibWrapper.OEMEncoding; Name = encoding.GetString(entryNameBuffer); UncompressedLength = (long)entryInfo.UncompressedSize; _fileAttributes = (FileAttributes)entryInfo.ExternalFileAttributes; IsDirectory = (_fileAttributes & FileAttributes.Directory) != 0; }
/// <summary> /// Reads the current entry into the specified memory stream /// </summary> /// <remarks> /// The data is put directly into the memory stream buffer /// No write operations are performed on the memory stream /// </remarks> /// <param name="memoryStream">memory stream</param> public void ReadCurrentEntry(MemoryStream memoryStream) { // Make sure the memory stream has enough capacity to hold the data memoryStream.SetLength(_currentZipEntry.UncompressedLength); byte[] memBuffer = memoryStream.GetBuffer(); int bytesRead = 0; int offset = 0; unsafe { fixed(byte *p = memBuffer) { for (; ;) { bytesRead = ZlibWrapper.ZlibUnzReadCurrentFile(_zipFileHandle, (IntPtr)p + offset, 4096); offset += bytesRead; if (bytesRead <= 0) { break; } } } } }
/// <summary> /// Initializes a instance of the <see cref="ZipReader"/> class for reading the zip file with the given name /// </summary> /// <param name="fileName">The path to the zip file that will be read</param> public ZipReader(string fileName) { FileName = fileName; _zipFileHandle = ZlibWrapper.ZlibUnzOpen(fileName); if (_zipFileHandle == IntPtr.Zero) { throw new ZipException(string.Format("Could not open zip file '{0}'", fileName)); } }
/// <summary> /// Opens the current selected Zip entry /// </summary> private void OpenCurrentEntry() { _currentZipEntry = new ZipEntry(_zipFileHandle); int result = ZlibWrapper.ZlibUnzOpenCurrentFile(_zipFileHandle); if (result < 0) { _currentZipEntry = null; throw new ZipException("Could not open entry for reading.", result); } }
/// <summary> /// Closes the current Zip entry /// </summary> private void CloseCurrentEntry() { if (_currentZipEntry == null) { return; } int result = ZlibWrapper.ZlibUnzCloseCurrentFile(_zipFileHandle); if (result < 0) { throw new ZipException("Could not close zip entry.", result); } _currentZipEntry = null; }
/// <summary> /// Closes the Zip file and releases the unmanaged resources /// </summary> private void CloseFile() { if (_zipFileHandle == IntPtr.Zero) { return; } try { CloseCurrentEntry(); } finally { int result = ZlibWrapper.ZlibUnzClose(_zipFileHandle); if (result < 0) { throw new ZipException("Could not close zip file.", result); } _zipFileHandle = IntPtr.Zero; } }