示例#1
0
        public static void ExtractToDirectory(string sourceArchiveFileName, string destinationDirectoryName)
        {
            using (var zipStream = new ZipInputStream(FileEx.OpenRead(sourceArchiveFileName)))
            {
                ZipEntry theEntry;
                byte[]   data = new byte[BufferSize];

                while ((theEntry = zipStream.GetNextEntry()) != null)
                {
                    var fileName = Path.GetFileName(theEntry.Name);
                    if (string.IsNullOrEmpty(fileName))
                    {
                        continue;
                    }

                    string file = Path.Combine(destinationDirectoryName, theEntry.Name);
                    DirectoryEx.CreateDirectory(Directory.GetParent(file));

                    using (FileStream fileStream = File.Create(file))
                    {
                        while (true)
                        {
                            var size = zipStream.Read(data, 0, data.Length);
                            if (size <= 0)
                            {
                                break;
                            }

                            fileStream.Write(data, 0, size);
                        }
                    }
                }
            }
        }
        static bool ReadFile(IntPtr buffer, string fileName, uint fileOffset, uint length, IntPtr userdata)
        {
            try
            {
                var fullPath      = Path.Combine(AppDataDir, fileName);
                var offset        = (int)fileOffset;
                var left          = (int)length;
                var data          = new byte[length];
                var splitFileName = string.Empty;
                var splitIndex    = 0;

                while (left > 0)
                {
                    Debug.Log("Merge from file:" + fullPath);

                    using (var stream = FileEx.OpenRead(fullPath))
                    {
                        if (stream == null)
                        {
                            Debug.LogError("File not exist:" + fullPath);
                            return(false);
                        }

                        stream.Seek(offset, SeekOrigin.Begin);
                        while (left > 0)
                        {
                            var ret = stream.Read(data, (int)length - left, left);
                            if (ret <= 0)
                            {
                                if (string.IsNullOrEmpty(splitFileName))
                                {
                                    TrySplitPath(fullPath, ref splitFileName, ref splitIndex);
                                }

                                if (string.IsNullOrEmpty(splitFileName))
                                {
                                    Debug.LogError(string.Format("stream read failed. {0}, offset:{1}, length{2}, left:{3}", fileName, offset, length, left));
                                    return(false);
                                }

                                fullPath = GetNextSplitPath(splitFileName, ref splitIndex);
                                offset   = 0;
                                break;
                            }
                            left -= ret;
                        }
                    }
                }

                Marshal.Copy(data, 0, buffer, data.Length);
                return(true);
            }
            catch (System.Exception e)
            {
                Debug.LogException(e);
                return(false);
            }
        }
 public UnzipProgress(string sourceArchiveFileName, string destinationDirectoryName)
 {
     SourceArchiveFile        = FileEx.OpenRead(sourceArchiveFileName);
     ZipStream                = new ZipInputStream(SourceArchiveFile);
     DestinationDirectoryName = destinationDirectoryName;
 }