Ejemplo n.º 1
0
        /// <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>
        bool MoveNext()
        {
            int result;

            if (_current == null)
            {
                result = Minizip.unzGoToFirstFile(_handle);
            }
            else
            {
                CloseCurrentEntry();
                result = Minizip.unzGoToNextFile(_handle);
            }

            if (result == ZipReturnCode.EndOfListOfFile)
            {
                // no more entries
                _current = null;
            }
            else if (result < 0)
            {
                throw new ZipException("MoveNext failed.", result);
            }
            else
            {
                // entry found
                OpenCurrentEntry();
            }

            return(_current != null);
        }
Ejemplo n.º 2
0
 /// <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   = Minizip.unzOpen(fileName);
     if (_handle == IntPtr.Zero)
     {
         string msg = String.Format("Could not open zip file '{0}'.", fileName);
         throw new ZipException(msg);
     }
 }
Ejemplo n.º 3
0
        /// <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 = Minizip.zipOpen(fileName, false /* false = create new, true = append */);
            if (_handle == IntPtr.Zero)
            {
                string msg = String.Format("Could not open zip file '{0}' for writing.", fileName);
                throw new ZipException(msg);
            }
        }
Ejemplo n.º 4
0
        /// <summary>Gets and sets the default compresion level for zip file entries.  See <see cref="CompressionMethod"/> for a partial list of values.</summary>
        //public int Level
        //{
        //    get { return _level; }
        //    set
        //    {
        //        if (value < -1 || value > 9)
        //        {
        //            throw new ArgumentOutOfRangeException("Level", value, "Level value must be between -1 and 9.");
        //        }
        //        _level = value;
        //    }
        //}

        ///// <summary>Gets and sets the default compresion method for zip file entries.  See <see cref="CompressionMethod"/> for a list of possible values.</summary>
        //public CompressionMethod Method
        //{
        //    get { return _method; }
        //    set { _method = value; }
        //}


        /// <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)
        {
            using (FixedArray fixedBuffer = new FixedArray(buffer))
            {
                int result = Minizip.zipWriteInFileInZip(_handle, fixedBuffer[index], (uint)count);
                if (result < 0)
                {
                    throw new ZipException("Write error.", result);
                }
            }
        }
Ejemplo n.º 5
0
        private void OpenCurrentEntry()
        {
            _current = new ZipEntry(_handle);
            int result = Minizip.unzOpenCurrentFile(_handle);

            if (result < 0)
            {
                _current = null;
                throw new ZipException("Could not open entry for reading.", result);
            }
        }
Ejemplo n.º 6
0
 private void CloseCurrentEntry()
 {
     if (_current != null)
     {
         int result = Minizip.zipCloseFileInZip(_handle);
         if (result < 0)
         {
             throw new ZipException("Could not close entry.", result);
         }
         _current = null;
     }
 }
Ejemplo n.º 7
0
 /// <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)
 {
     using (FixedArray fixedBuff = new FixedArray(buffer))
     {
         int bytesRead = Minizip.unzReadCurrentFile(_handle, fixedBuff[index], (uint)count);
         if (bytesRead < 0)
         {
             throw new ZipException("Error reading zip entry.", bytesRead);
         }
         return(bytesRead);
     }
 }
Ejemplo n.º 8
0
 private void CloseFile()
 {
     if (_handle != IntPtr.Zero)
     {
         try
         {
             CloseCurrentEntry();
         }
         finally
         {
             int result = Minizip.unzClose(_handle);
             if (result < 0)
             {
                 throw new ZipException("Could not close zip file.", result);
             }
             _handle = IntPtr.Zero;
         }
     }
 }
Ejemplo n.º 9
0
 private void CloseFile()
 {
     if (_handle != IntPtr.Zero)
     {
         try
         {
             CloseCurrentEntry();
         }
         finally
         {
             //file comment is for some weird reason ANSI, while entry name + comment is OEM...
             int result = Minizip.zipClose(_handle, _comment);
             if (result < 0)
             {
                 throw new ZipException("Could not close zip file.", result);
             }
             _handle = IntPtr.Zero;
         }
     }
 }
Ejemplo n.º 10
0
        /// <summary>Creates a new Zip file entry reading values from a zip file.</summary>
        internal ZipEntry(IntPtr handle)
        {
            ZipEntryInfo64 entryInfo = new ZipEntryInfo64();
            int            result    = Minizip.unzGetCurrentFileInfo64(handle, out entryInfo, null, 0, null, 0, null, 0);

            if (result != 0)
            {
                throw new ZipException("Could not read entry from zip file " + Name, result);
            }

            _extraField = new byte[entryInfo.ExtraFieldLength];
            byte[] entryNameBuffer = new byte[entryInfo.FileNameLength];
            byte[] commentBuffer   = new byte[entryInfo.CommentLength];

            result = Minizip.unzGetCurrentFileInfo64(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);
            }

            this._UTF8Encoding = BitFlag.IsSet(entryInfo.Flag, ZipEntryFlag.UTF8);
            Encoding encoding = this._UTF8Encoding ? Encoding.UTF8 : Minizip.OEMEncoding;

            _name = encoding.GetString(entryNameBuffer);
            //null or empty string if empty buffer?
            _comment            = encoding.GetString(commentBuffer);
            _crc                = entryInfo.Crc;
            _compressedLength   = (long)entryInfo.CompressedSize;
            _uncompressedLength = (long)entryInfo.UncompressedSize;
            _method             = (CompressionMethod)entryInfo.CompressionMethod;
            _modifiedTime       = entryInfo.ZipDateTime;
            _fileAttributes     = (FileAttributes)entryInfo.ExternalFileAttributes;
            _isDirectory        = InterpretIsDirectory();
        }
Ejemplo n.º 11
0
        /// <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)
        {
            //Close previous entry (if any).
            //Will trigger write of central dir info for previous file and may throw.
            CloseCurrentEntry();

            ZipFileEntryInfo info = new ZipFileEntryInfo();

            info.ZipDateTime            = entry.ModifiedTime;
            info.ExternalFileAttributes = (uint)entry.GetFileAttributesForZip();

            byte[] extra       = null;
            uint   extraLength = 0;

            if (entry.ExtraField != null)
            {
                extra       = entry.ExtraField;
                extraLength = (uint)entry.ExtraField.Length;
            }

            string nameForZip = entry.GetNameForZip();

            uint flagBase = 0;

            if (entry.UTF8Encoding)
            {
                flagBase |= ZipEntryFlag.UTF8;
            }
            else
            {
                if (!nameForZip.IsAscii())
                {
                    throw new ArgumentException("Name can only contain Ascii 8 bit characters.");
                }
                if (entry.Comment != null && !entry.Comment.IsAscii())
                {
                    throw new ArgumentException("Comment can only contain Ascii 8 bit characters.");
                }
            }

            Encoding encoding = entry.UTF8Encoding ? Encoding.UTF8 : Minizip.OEMEncoding;

            byte[] name    = encoding.GetBytes(nameForZip + Char.MinValue);          // add nullterm
            byte[] comment = null;
            if (entry.Comment != null)
            {
                comment = encoding.GetBytes(entry.Comment + Char.MinValue);                 // add nullterm
            }
            int result = Minizip.zipOpenNewFileInZip4_64(
                _handle,
                name,
                ref info,
                extra,
                extraLength,
                null,
                0,
                comment,                 //null is ok here
                (int)entry.Method,
                entry.Level,
                flagBase,
                entry.Zip64);

            if (result < 0)
            {
                throw new ZipException("AddEntry error.", result);
            }

            _current = entry;
        }