示例#1
0
    void StartDecompress()
    {
        if (m_startDecompress)
        {
            return;
        }

        m_startDecompress = true;

        string endFilePath = PathUtils.MakeFilePath("http_end.txt", PathUtils.PathType.MobileDiskWrite);

        if (File.Exists(endFilePath))
        {
            return;
        }

        Loom.RunAsync(() => {
            FileStream fs      = null;
            ZipInputStream zis = null;
            try
            {
                string path = PathUtils.MakeFilePath(GameConfig.HTTP_ZIP_FILE, PathUtils.PathType.MobileDiskWrite);
                fs          = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                zis         = new ZipInputStream(fs);
                ZipEntry ze = null;
                byte[] data = new byte[2048];
                //long lastZipIndex = 0;

                //string infoFilePath = PathUtils.MakeFilePath(GameConfig.LOCAL_DOWNLOAD_INFO_FILE, PathUtils.PathType.MobileDiskWrite);
                //if (File.Exists(infoFilePath))
                //{
                //    using (FileStream infoStream = File.OpenRead(infoFilePath))
                //    {
                //        if (infoStream != null)
                //        {
                //            byte[] index = new byte[infoStream.Length];
                //            infoStream.Read(index, 0, index.Length);
                //            string content = System.Text.Encoding.Default.GetString(index);
                //            DownloadFileInfo downloadFileInfo = JsonFx.Json.JsonReader.Deserialize<DownloadFileInfo>(content);
                //            lastZipIndex = downloadFileInfo.totalSize;
                //            for (int i = 0; i < downloadFileInfo.ids.Length; i++)
                //            {
                //                downloadedFiles.Add(downloadFileInfo.ids[i], 1);
                //            }
                //            fs.Seek(lastZipIndex, SeekOrigin.Begin);
                //        }
                //    }
                //}

                fs.Seek(lastZipIndex, SeekOrigin.Begin);
                while (!GameWorld.isQuit)
                {
                    try
                    {
                        ze = zis.GetNextEntry();
                        if (null != ze)
                        {
                            using (FileStream stream = File.Create(PathUtils.MakeFilePath(ze.Name, PathUtils.PathType.MobileDiskWrite)))
                            {
                                long totalSize = ze.Size;
                                while (!GameWorld.isQuit && totalSize > 0)
                                {
                                    int size   = zis.Read(data, 0, data.Length);
                                    totalSize -= size;
                                    if (size > 0)
                                    {
                                        stream.Write(data, 0, size);
                                    }
                                    else
                                    {
                                        System.Threading.Thread.Sleep(10);
                                    }
                                }

                                if (totalSize == 0)
                                {
                                    lastZipIndex += ze.headSize;
                                    lastZipIndex += zis.GetTotalIn();
                                    GameUtils.AddItemToDic(downloadedFiles, ze.Name, 1);
                                }
                            }
                        }
                        else if (m_downloadComplated)
                        {
                            break;
                        }
                    }
                    catch
                    {
                        if (m_downloadComplated)
                        {
                            break;
                        }
                        zis.Reset();
                        fs.Seek(lastZipIndex, SeekOrigin.Begin);
                    }
                    System.Threading.Thread.Sleep(10);
                }
            }
            catch (Exception e)
            {
                Loom.QueueOnMainThread(() => {
                    Debug.LogError(e.Message + "\n" + e.StackTrace);
                });
            }
            finally
            {
                if (null != zis)
                {
                    zis.Close();
                }

                if (null != fs)
                {
                    fs.Close();
                }

                using (FileStream infoStream = new FileStream(PathUtils.MakeFilePath(GameConfig.LOCAL_DOWNLOAD_INFO_FILE, PathUtils.PathType.MobileDiskWrite), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    DownloadFileInfo info = new DownloadFileInfo()
                    {
                        totalSize = lastZipIndex, ids = downloadedFiles.Keys.ToArray <string>()
                    };
                    string content = JsonFx.Json.JsonWriter.Serialize(info);
                    byte[] index   = Encoding.Default.GetBytes(content);
                    infoStream.Write(index, 0, index.Length);
                }
            }
        });
    }