/// <summary>Advances the enumerator to the next element of the collection.</summary> /// <summary>Sets <see cref="Current"/> to the next zip entry.</summary> /// <returns><c>true</c> if the next entry is not <c>null</c>; otherwise <c>false</c>.</returns> public bool MoveNext() { // close any open entry CloseEntry(); int result; if (_current == null) { result = ZipLib.unzGoToFirstFile(_handle); } else { result = ZipLib.unzGoToNextFile(_handle); } if (result < 0) { // last entry found - not an exceptional case _current = null; } else { // entry found OpenEntry(); } return(_current != null); }
/// <summary>Creates a new zip entry in the directory and positions the stream to the start of the entry data.</summary> /// <param name="entry">The zip entry to be written.</param> /// <remarks>Closes the current entry if still active.</remarks> public void AddEntry(ZipEntry entry) { ZipFileEntryInfo info; info.DateTime = entry.ModifiedTime; int result; unsafe { byte[] extra = null; uint extraLength = 0; if (entry.ExtraField != null) { extra = entry.ExtraField; extraLength = (uint)entry.ExtraField.Length; } result = ZipLib.zipOpenNewFileInZip( _handle, entry.Name, &info, extra, extraLength, null, 0, entry.Comment, (int)entry.Method, entry.Level); } _entryOpen = true; }
private void CloseEntry() { if (_entryOpen) { int result = ZipLib.zipCloseFileInZip(_handle); _entryOpen = false; } }
/// <summary>Initializes a instance of the <see cref="ZipReader"/> class for reading the zip file with the given name.</summary> /// <param name="fileName">The name of zip file that will be read.</param> public ZipReader(string fileName) { _fileName = fileName; _handle = ZipLib.unzOpen(fileName); if (_handle == IntPtr.Zero) { string msg = String.Format("Could not open zip file '{0}'.", fileName); throw new ZipException(msg); } }
/// <summary>Initializes a new instance fo the <see cref="ZipWriter"/> class with a specified file name. Any Existing file will be overwritten.</summary> /// <param name="fileName">The name of the zip file to create.</param> public ZipWriter(string fileName) { _fileName = fileName; _handle = ZipLib.zipOpen(fileName, 0); if (_handle == IntPtr.Zero) { string msg = String.Format("Could not open zip file '{0}' for writing.", fileName); throw new ZipException(msg); } }
private void OpenEntry() { _current = new ZipEntry(_handle); int result = ZipLib.unzOpenCurrentFile(_handle); if (result < 0) { _current = null; throw new ZipException("Could not open entry for reading."); } _entryOpen = true; }
/// <summary>Seek to the specified entry.</summary> /// <param name="entryName">The name of the entry to seek to.</param> public void Seek(string entryName) { CloseEntry(); int result = ZipLib.unzLocateFile(_handle, entryName, 0); if (result < 0) { string msg = String.Format("Could not locate entry named '{0}'.", entryName); throw new ZipException(msg); } OpenEntry(); }
private void CloseFile() { if (_handle != IntPtr.Zero) { CloseEntry(); int result = ZipLib.unzClose(_handle); if (result < 0) { throw new ZipException("Could not close zip file."); } _handle = IntPtr.Zero; } }
/// <summary>Uncompress a block of bytes from the current zip entry and writes the data in a given buffer.</summary> /// <param name="buffer">The array to write data into.</param> /// <param name="index">The byte offset in <paramref name="buffer"/> at which to begin writing.</param> /// <param name="count">The maximum number of bytes to read.</param> public int Read(byte[] buffer, int index, int count) { if (index != 0) { throw new ArgumentException("index", "Only index values of zero currently supported."); } int bytesRead = ZipLib.unzReadCurrentFile(_handle, buffer, (uint)count); if (bytesRead < 0) { throw new ZipException("Error reading zip entry."); } return(bytesRead); }
/// <summary>Creates a new Zip file entry reading values from a zip file.</summary> internal ZipEntry(IntPtr handle) { ZipEntryInfo entryInfo; int result = 0; unsafe { result = ZipLib.unzGetCurrentFileInfo(handle, &entryInfo, null, 0, null, 0, null, 0); } if (result != 0) { throw new ZipException("Could not read entries from zip file " + Name); } ExtraField = new byte[entryInfo.ExtraFieldLength]; sbyte[] entryNameBuffer = new sbyte[entryInfo.FileNameLength]; sbyte[] commentBuffer = new sbyte[entryInfo.CommentLength]; unsafe { result = ZipLib.unzGetCurrentFileInfo(handle, &entryInfo, entryNameBuffer, (uint)entryNameBuffer.Length, ExtraField, (uint)ExtraField.Length, commentBuffer, (uint)commentBuffer.Length); } if (result != 0) { throw new ZipException("Could not read entries from zip file " + Name); } _name = ZipLib.AnsiToString(entryNameBuffer); _comment = ZipLib.AnsiToString(commentBuffer); _crc = entryInfo.Crc; _compressedLength = entryInfo.CompressedSize; _uncompressedLength = entryInfo.UncompressedSize; _method = (CompressionMethod)entryInfo.CompressionMethod; _modifiedTime = new DateTime( (int)entryInfo.DateTime.Year, (int)entryInfo.DateTime.Month + 1, (int)entryInfo.DateTime.Day, (int)entryInfo.DateTime.Hours, (int)entryInfo.DateTime.Minutes, (int)entryInfo.DateTime.Seconds); }
private void CloseEntry() { if (_entryOpen) { int result = ZipLib.unzCloseCurrentFile(_handle); if (result < 0) { switch ((ErrorCode)result) { case ErrorCode.CrcError: throw new ZipException("All the file was read but the CRC did not match."); default: throw new ZipException("Could not close zip entry."); } } _entryOpen = false; } }
/// <summary>Compress a block of bytes from the given buffer and writes them into the current zip entry.</summary> /// <param name="buffer">The array to read data from.</param> /// <param name="index">The byte offset in <paramref name="buffer"/> at which to begin reading.</param> /// <param name="count">The maximum number of bytes to write.</param> public void Write(byte[] buffer, int index, int count) { int result = ZipLib.zipWriteInFileInZip(_handle, buffer, (uint)count); }