public override List <string> GetZipDirInfo(string zipFileName)
        {
            //IntPtr zhandle = ZipLib.unzOpen(zipFileName);
            FileStream    fs         = new FileStream(zipFileName, FileMode.Open, FileAccess.Read);
            BinaryReader  br         = new BinaryReader(fs);
            List <string> zipDirInfo = new List <string>();

            try
            {
                while (br.BaseStream.Position < br.BaseStream.Length)
                {
                    if (br.ReadByte() == 0x50)
                    {
                        if (br.ReadByte() == 0x4b)
                        {
                            if (br.ReadByte() == 0x01)
                            {
                                if (br.ReadByte() == 0x02)
                                {
                                    // 22=2version made by+2version needed to extract+2general purpose bit flag+2compression method+2last mod file time+
                                    // 2last mode file date+4crc-32+4compressedsize+4uncompressedsize
                                    br.BaseStream.Position += 24;
                                    int filenameLength = br.ReadInt16();
                                    // br.BaseStream.Position += 2;

                                    // 16=2extra field length+2file comment length+2disk number start+4external file attributes+4relative offset of local header
                                    br.BaseStream.Position += 16;

                                    byte[] buffer = new byte[filenameLength];
                                    br.BaseStream.Read(buffer, 0, filenameLength);

                                    string fileName = ZipLib.ResolvePath(Encoding.GetEncoding("UTF-8").GetString(buffer));
                                    zipDirInfo.Add(fileName);
                                }
                            }
                        }
                    }
                }

                return(zipDirInfo);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
                if (br != null)
                {
                    br.Close();
                }
            }
        }
        public ZlibZipWriter(string path)
        {
            string resolvedPath = ZipLib.ResolvePath(path);

            this.handle = ZipLib.zipOpen(resolvedPath, 0);
            if (this.handle == IntPtr.Zero)
            {
                throw new ZipException("Error while creating file: " + path);
            }
            this.entryOpened = false;
        }
 public override void Close()
 {
     if (handle != IntPtr.Zero)
     {
         int result = ZipLib.unzClose(this.handle);
         handle = IntPtr.Zero;
         // Question: should we raise this exception ?
         if (result != 0)
         {
             throw new ZipException("Error closing file - Errorcode: " + result);
         }
     }
 }
        internal ZlibZipReader(string path)
        {
            string resolvedPath = ZipLib.ResolvePath(path);

            if (!File.Exists(resolvedPath))
            {
                throw new FileNotFoundException("File does not exist:" + path);
            }
            this.handle = ZipLib.unzOpen(resolvedPath);
            if (handle == IntPtr.Zero)
            {
                throw new ZipException("Unable to open ZIP file:" + path);
            }
        }
        public override void Write(byte[] buffer, int offset, int count)
        {
            int result;

            if (offset != 0)
            {
                byte[] newBuffer = new byte[count];
                Array.Copy(buffer, offset, newBuffer, 0, count);
                result = ZipLib.zipWriteInFileInZip(handle, newBuffer, (uint)count);
            }
            else
            {
                result = ZipLib.zipWriteInFileInZip(handle, buffer, (uint)count);
            }

            if (result < 0)
            {
                throw new ZipException("Error while writing entry - Errorcode: " + result);
            }
        }
        public override void AddEntry(string relativePath)
        {
            string           resolvedPath = ZipLib.ResolvePath(relativePath);
            ZipFileEntryInfo info;

            info.DateTime = DateTime.Now;

            if (this.entryOpened)
            {
                ZipLib.zipCloseFileInZip(this.handle);
                this.entryOpened = false;
            }

            int result = ZipLib.zipOpenNewFileInZip(this.handle, resolvedPath, out info, null, 0, null, 0, String.Empty, (int)CompressionMethod.Deflated, (int)CompressionLevel.Default);

            if (result < 0)
            {
                throw new ZipException("Error while opening entry for writing: " + relativePath + " - Errorcode: " + result);
            }
            this.entryOpened = true;//经典,用此标志来标示哪个文件当前可写
        }
        public override Stream GetEntry(string relativePath)
        {
            string resolvedPath = ZipLib.ResolvePath(relativePath);

            if (ZipLib.unzLocateFile(this.handle, resolvedPath, 0) != 0)
            {
                throw new ZipEntryNotFoundException("Entry not found:" + relativePath);
            }

            ZipEntryInfo entryInfo = new ZipEntryInfo();
            int          result    = ZipLib.unzGetCurrentFileInfo(this.handle, out entryInfo, null, 0, null, 0, null, 0);

            if (result != 0)
            {
                throw new ZipException("Error while reading entry info: " + relativePath + " - Errorcode: " + result);
            }

            result = ZipLib.unzOpenCurrentFile(this.handle);
            if (result != 0)
            {
                throw new ZipException("Error while opening entry: " + relativePath + " - Errorcode: " + result);
            }

            byte[] buffer    = new byte[entryInfo.UncompressedSize];
            int    bytesRead = 0;

            if ((bytesRead = ZipLib.unzReadCurrentFile(this.handle, buffer, (uint)entryInfo.UncompressedSize)) < 0)
            {
                throw new ZipException("Error while reading entry: " + relativePath + " - Errorcode: " + result);
            }

            result = ZipLib.unzCloseCurrentFile(handle);
            if (result != 0)
            {
                throw new ZipException("Error while closing entry: " + relativePath + " - Errorcode: " + result);
            }

            return(new MemoryStream(buffer, 0, bytesRead));
        }
 public override void Close()
 {
     if (handle != IntPtr.Zero)
     {
         int result;
         if (this.entryOpened)
         {
             result = ZipLib.zipCloseFileInZip(this.handle);
             if (result != 0)
             {
                 throw new ZipException("Error while closing entry - Errorcode: " + result);
             }
             this.entryOpened = false;
         }
         result = ZipLib.zipClose(this.handle, "");
         handle = IntPtr.Zero;
         // Should we raise this exception ?
         if (result != 0)
         {
             throw new ZipException("Error while closing ZIP file - Errorcode: " + result);
         }
     }
 }
        public override void ExtractOfficeDocument(string zipFileName, string directory)
        {
            IntPtr       zhandle = ZipLib.unzOpen(zipFileName);
            FileStream   fs      = new FileStream(zipFileName, FileMode.Open, FileAccess.Read);
            BinaryReader br      = new BinaryReader(fs);
            int          i       = 0;
            int          flag    = 0;
            int          flag2   = 0;
            int          count   = 1;

            try
            {
                while (br.BaseStream.Position < br.BaseStream.Length)
                {
                    if (br.ReadByte() == 0x50)
                    {
                        if (br.ReadByte() == 0x4b)
                        {
                            if (br.ReadByte() == 0x03)
                            {
                                if (br.ReadByte() == 0x04)
                                {
                                    br.BaseStream.Position += 22;
                                    int filenameLength = br.ReadInt16();
                                    br.BaseStream.Position += 2;
                                    byte[] buffer = new byte[filenameLength];
                                    br.BaseStream.Read(buffer, 0, filenameLength);
                                    br.BaseStream.Position -= (26 + filenameLength);
                                    string        fullFileName = directory + ZipLib.ResolvePath(Encoding.GetEncoding("UTF-8").GetString(buffer));
                                    DirectoryInfo dir          = new DirectoryInfo(fullFileName);
                                    if (fullFileName.Contains("/"))
                                    {
                                        string tempdir = "";
                                        while (tempdir != dir.Root.FullName)
                                        {
                                            tempdir = dir.Parent.FullName;
                                            dir     = new DirectoryInfo(dir.Parent.FullName);
                                            if (!Directory.Exists(tempdir))
                                            {
                                                Directory.CreateDirectory(tempdir);
                                            }
                                        }
                                    }

                                    FileStream writeFile = File.Create(fullFileName);
                                    if (i < count)
                                    {
                                        try
                                        {
                                            i++;
                                            if (flag == 0)
                                            {
                                                ZipLib.unzGoToFirstFile(zhandle);
                                                flag = 1;
                                            }
                                            else
                                            {
                                                int fir = ZipLib.unzGoToNextFile(zhandle);
                                            }
                                            ZipEntryInfo zipentryInfo;
                                            ZipLib.unzGetCurrentFileInfo(zhandle, out zipentryInfo, null, 0, null, 0, null, 0);
                                            ZipLib.unzOpenCurrentFile(zhandle);
                                            ZipFileInfo zipfileinfo;
                                            ZipLib.unzGetGlobalInfo(zhandle, out zipfileinfo);
                                            if (flag2 == 0)
                                            {
                                                count = (int)zipfileinfo.EntryCount;
                                                flag2 = 1;
                                            }
                                            byte[] buffer2 = new byte[zipentryInfo.UncompressedSize];
                                            ZipLib.unzReadCurrentFile(zhandle, buffer2, (uint)zipentryInfo.UncompressedSize);
                                            writeFile.Write(buffer2, 0, (int)zipentryInfo.UncompressedSize);
                                            br.BaseStream.Position += zipentryInfo.CompressedSize;
                                        }
                                        catch (Exception ex)
                                        {
                                            throw ex;
                                        }
                                        finally
                                        {
                                            if (writeFile != null)
                                            {
                                                writeFile.Close();
                                            }
                                            if (ZipLib.unzCloseCurrentFile(zhandle) != 0)
                                            {
                                                ZipLib.unzCloseCurrentFile(zhandle);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
                if (br != null)
                {
                    br.Close();
                }
            }
        }