コード例 #1
0
        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));
        }
コード例 #2
0
        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();
                }
            }
        }