コード例 #1
0
        /// <summary>指定されたエクセルブックファイルに含まれる画像を圧縮します。</summary>
        /// <param name="sourceFilePath">ファイルパス</param>
        /// <param name="targetInnerPath">処理対象内部パス</param>
        internal void QuantFromZipFile(string sourceFilePath, string targetInnerPath = "")
        {
            string reg = this.IsConvertToPng ? @"{0}.*\.png|{0}.*\.jpeg|{0}.*\.jpg|{0}.*\.bmp" : @"{0}.*\.png";

            reg = string.Format(reg, targetInnerPath);
            Regex regex = new Regex(reg);

            Console.WriteLine("減色を開始します:{0}", sourceFilePath);

            string distinationFileName = Path.GetFileNameWithoutExtension(sourceFilePath) + ".zip";

            string.Format(Properties.Settings.Default.FileNameTemplate, distinationFileName);
            string distinationFilePath = Path.GetDirectoryName(sourceFilePath);

            distinationFilePath = Path.Combine(distinationFilePath, distinationFileName);
            this.OutputFilePath = distinationFilePath;

            using (RarArchive archive = RarArchive.Open(sourceFilePath))
                using (ZipArchive distArchive = new ZipArchive(new FileStream(distinationFilePath, FileMode.Create, FileAccess.Write), ZipArchiveMode.Create))
                    using (PngQuant pngquant = new PngQuant())
                    {
                        foreach (RarArchiveEntry entry in archive.Entries)
                        {
                            if (regex.IsMatch(entry.Key.ToLower()) == false)
                            {
                                ZipArchiveEntry distEntry = distArchive.CreateEntry(entry.Key);
                                using (Stream srcStream = entry.OpenEntryStream())
                                    using (Stream distStream = distEntry.Open())
                                    {
                                        srcStream.CopyTo(distStream);
                                    }

                                continue;
                            }

                            // 解凍して一時ファイルを作成し、減色する
                            Stream tranStream;
                            using (Stream s = entry.OpenEntryStream())
                            {
                                tranStream = pngquant.Subtractive(s);
                            }

                            // 減色したファイルをブックに再設定
                            string distEntryName = PathUtils.ChangeExtension(entry.Key, ".png");
                            var    e             = distArchive.CreateEntry(distEntryName, CompressionLevel.NoCompression);
                            using (var stream = e.Open())
                            {
                                tranStream.CopyTo(stream);
                            }

                            Console.WriteLine("・{0}", entry.Key);

                            // 変換イベントを発生させる
                            this.FileNameChanged?.Invoke(this, new FileNameChangedEventArgs(entry.Key, distEntryName));
                        }
                    }
        }
コード例 #2
0
 public PngQuantTransformer(PngQuantOptions options = null)
 {
     _pngquant = new PngQuant(options);
 }