예제 #1
0
        /// <summary>
        /// 压缩文件解包
        /// </summary>
        private void ZipUnpack(HttpContext Context)
        {
            MemoryStream      MS                = default(MemoryStream);
            SevenZipExtractor Extractor         = default(SevenZipExtractor);
            Hashtable         ExportTable       = new Hashtable();
            Hashtable         FileTable         = new Hashtable();
            string            PackageId         = "";
            string            PackageFolderPath = "";
            string            UnpackFolderPath  = "";
            string            StorageFolderPath = "";
            int    Id         = 0;
            string FolderPath = "";
            string CodeId     = "";
            string Name       = "";
            string Extension  = "";
            string FilePath   = "";
            string Password   = "";

            byte[] Bytes = {};
            int    Item  = 0;
            int    Index = 0;

            PackageId = Guid.NewGuid().ToString();

            PackageFolderPath = Base.Common.PathCombine(Context.Server.MapPath("/storage/file/temp/"), PackageId);

            if (Directory.Exists(PackageFolderPath) == false)
            {
                Directory.CreateDirectory(PackageFolderPath);
            }

            StorageFolderPath = Context.Server.MapPath("/storage/file/");

            try
            {
                if (Base.Common.IsNumeric(Context.Request.QueryString["Id"]) == true)
                {
                    Id = Context.Request.QueryString["Id"].TypeInt();
                }
                else
                {
                    return;
                }

                Password = Context.Request.QueryString["Password"].TypeString();

                if (string.IsNullOrEmpty(Password) == false)
                {
                    if (Base.Common.StringCheck(Password, @"^[\S]{1,32}$") == false)
                    {
                        return;
                    }
                }

                if (AppCommon.PurviewCheck(Id, false, "downloader", ref Conn) == false)
                {
                    Context.Response.Write("no-permission");
                    return;
                }

                for (Index = 0; Index < Context.Request.QueryString.GetValues("Item").Length; Index++)
                {
                    if (Base.Common.IsNumeric(Context.Request.QueryString.GetValues("Item")[Index]) == true)
                    {
                        Item = Context.Request.QueryString.GetValues("Item")[Index].TypeInt();
                    }
                    else
                    {
                        continue;
                    }

                    ExportTable.Add(Item, true);
                }

                Base.Data.SqlDataToTable("Select DBS_Id, DBS_Folder, DBS_FolderPath, DBS_CodeId, DBS_Name, DBS_Extension, DBS_Recycle From DBS_File Where DBS_Folder = 0 And DBS_Recycle = 0 And DBS_Id = " + Id, ref Conn, ref FileTable);

                if (FileTable["Exist"].TypeBool() == false)
                {
                    return;
                }
                else
                {
                    FolderPath = FileTable["DBS_FolderPath"].TypeString();
                    CodeId     = FileTable["DBS_CodeId"].TypeString();
                    Name       = FileTable["DBS_Name"].TypeString();
                    Extension  = FileTable["DBS_Extension"].TypeString();
                }

                FileTable.Clear();

                if (Extension != ".7z" && Extension != ".rar" && Extension != ".zip")
                {
                    return;
                }

                FilePath = Base.Common.PathCombine(Context.Server.MapPath("/storage/file/"), FolderPath.Substring(1), CodeId + Extension);

                if (File.Exists(FilePath) == false)
                {
                    return;
                }

                UnpackFolderPath = Base.Common.PathCombine(PackageFolderPath, Name);

                if (Directory.Exists(UnpackFolderPath) == false)
                {
                    Directory.CreateDirectory(UnpackFolderPath);
                }

                Bytes = ReadFileBytes(FilePath, CodeId);

                if (Bytes.Length == 0)
                {
                    return;
                }

                MS = new MemoryStream(Bytes);

                SevenZipCompressor.SetLibraryPath(Context.Server.MapPath("/bin/7z64.dll"));

                if (string.IsNullOrEmpty(Password) == true)
                {
                    Extractor = new SevenZipExtractor(MS);
                }
                else
                {
                    Extractor = new SevenZipExtractor(MS, Password);
                }

                for (Index = 0; Index < Extractor.ArchiveFileData.Count; Index++)
                {
                    if (Context.Response.IsClientConnected == false)
                    {
                        return;
                    }

                    if (ExportTable[Index].TypeBool() == true)
                    {
                        try
                        {
                            Extractor.ExtractFiles(UnpackFolderPath, Extractor.ArchiveFileData[Index].Index);
                        }
                        catch (Exception)
                        {
                        }
                    }
                }

                if (Extractor.Check() == false)
                {
                    Context.Response.Write("wrong-password");
                    return;
                }

                Extractor = null;

                MS.Dispose();

                ZipScan(0, PackageFolderPath, StorageFolderPath, Context);
            }
            catch (Exception ex)
            {
                AppCommon.Error(ex);

                Context.Response.StatusCode = 500;
            }
            finally
            {
                if (Base.Common.IsNothing(MS) == false)
                {
                    MS.Dispose();
                }

                if (Base.Common.IsNothing(Extractor) == false)
                {
                    Extractor = null;
                }

                Directory.Delete(PackageFolderPath, true);

                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }