Exemple #1
0
        private static void ExtractFilesInBinaryTable(InstallPackage installPackage, ICollection <string> names, string tableName, string path)
        {
            if (!Directory.Exists(path))
            {
                throw new ArgumentException(string.Format("The path specified does not exist. {0}", path), "path");
            }

            View binaryView = installPackage.OpenView("Select `Name`, `Data` FROM `{0}`", tableName);

            binaryView.Execute();

            ICollection <string> createdFiles = new List <string>(100);

            for (Record binaryRec = binaryView.Fetch(); binaryRec != null; binaryRec = binaryView.Fetch())
            {
                string binaryKey  = (string)binaryRec[1];
                Stream binaryData = (Stream)binaryRec[2];

                if (null != names && !names.Contains(binaryKey))
                {
                    continue;                                              //Skip unspecified values
                }
                createdFiles.Add(binaryKey);

                FileInfo binaryFile = new FileInfo(Path.Combine(path, binaryKey));
                using (FileStream fs = binaryFile.Create())
                {
                    Stream tempBuffer = new MemoryStream((int)binaryFile.Length);
                    for (int a = binaryData.ReadByte(); a != -1; a = binaryData.ReadByte())
                    {
                        tempBuffer.WriteByte((byte)a);
                    }
                    tempBuffer.Seek(0, SeekOrigin.Begin);
                    for (int a = tempBuffer.ReadByte(); a != -1; a = tempBuffer.ReadByte())
                    {
                        fs.WriteByte((byte)a);
                    }
                }
            }

            InstallPackage.ClearReadOnlyAttribute(path, createdFiles);
        }