public static IWriter Open(Stream stream, ArchiveType archiveType, WriterOptions writerOptions)
        {
            switch (archiveType)
            {
            case ArchiveType.GZip:
            {
                if (writerOptions.CompressionType != CompressionType.GZip)
                {
                    throw new InvalidFormatException("GZip archives only support GZip compression type.");
                }
                return(new GZipWriter(stream, writerOptions.LeaveStreamOpen));
            }

            case ArchiveType.Zip:
            {
                return(new ZipWriter(stream, new ZipWriterOptions(writerOptions)));
            }

            case ArchiveType.Tar:
            {
                return(new TarWriter(stream, writerOptions));
            }

            default:
            {
                throw new NotSupportedException("Archive Type does not have a Writer: " + archiveType);
            }
            }
        }
Example #2
0
 protected AbstractWriter(ArchiveType type, WriterOptions writerOptions)
 {
     WriterType    = type;
     WriterOptions = writerOptions;
 }
Example #3
0
        private void convertToZip(string filename)
        {
            if (string.IsNullOrEmpty(SelectedFilePath))
            {
                return;
            }

            if (File.Exists(filename))
            {
                if (MessageBox.Show("Overwrite?", "Save", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    File.Delete(filename);
                }
                else
                {
                    return;
                }
            }

            Task <int> task1 = Task.Run(() => {
                IsVisibleProgress     = Visibility.Visible;
                IsEnableProgressClose = false;

                var opts             = new SharpCompress.Writers.WriterOptions(CompressionType.Deflate);
                opts.ArchiveEncoding = new SharpCompress.Common.ArchiveEncoding();
                //opts.ArchiveEncoding.Default = System.Text.Encoding.GetEncoding("shift_jis");
                opts.ArchiveEncoding.Default = System.Text.Encoding.UTF8;

                using (var zip = File.OpenWrite(filename))
                    using (var zipWriter = WriterFactory.Open(zip, ArchiveType.Zip, opts))
                    {
                        ProgressOutFileName = SelectedFilePath;

                        IArchive archive = ArchiveFactory.Open(SelectedFilePath);
                        var entries      = archive.Entries.Where(e =>
                                                                 e.IsDirectory == false && (
                                                                     Path.GetExtension(e.Key).Equals(".jpg") ||
                                                                     Path.GetExtension(e.Key).Equals(".jpeg") ||
                                                                     Path.GetExtension(e.Key).Equals(".png") ||
                                                                     Path.GetExtension(e.Key).Equals(".bmp")));

                        var list      = entries.ToList();
                        ProgressMax   = list.Count;
                        ProgressValue = 0;
                        foreach (var item in entries.ToList())
                        {
                            ProgressFileName = item.Key;
                            var outStream    = new MemoryStream();

#if IMAGE_RESIZER
                            var instructions    = new Instructions();
                            instructions.Format = "jpg";

                            if (IsResize)
                            {
                                instructions.Width  = ImageWidth;
                                instructions.Height = ImageHeight;
                                instructions.Mode   = FitMode.Max;
                            }

                            if (IsCompress)
                            {
                                if (IsGrayscale)
                                {
                                    instructions.Grayscale = GrayscaleMode.Flat; // TODO: check mode
                                }

                                instructions.JpegSubsampling = JpegSubsamplingMode.Y4Cb2Cr0;
                                instructions.JpegQuality     = JpegQuality;
                            }

                            ImageBuilder.Current.Build(new ImageJob(item.OpenEntryStream(), outStream, instructions, false, true));
#endif
#if IMAGE_SHARP
                            using (Image image = Image.Load(item.OpenEntryStream()))
                            {
                                if (IsResize)
                                {
                                    image.Mutate(x => x
                                                 .Resize(new ResizeOptions
                                    {
                                        Mode = ResizeMode.Min,
                                        Size = new Size(ImageWidth, ImageHeight),
                                    })
                                                 );
                                }

                                if (IsCompress)
                                {
                                    if (IsGrayscale)
                                    {
                                        image.Mutate(x => x
                                                     .Grayscale()
                                                     );
                                    }

                                    // Use ImageSharp
                                    if (false)
                                    {
                                        var enc       = new SixLabors.ImageSharp.Formats.Jpeg.JpegEncoder();
                                        enc.Quality   = JpegQuality;
                                        enc.Subsample = SixLabors.ImageSharp.Formats.Jpeg.JpegSubsample.Ratio444;
                                        if (ImageFormat == ImgFmt.IMG_444)
                                        {
                                            enc.Subsample = SixLabors.ImageSharp.Formats.Jpeg.JpegSubsample.Ratio444;
                                        }
                                        else if (ImageFormat == ImgFmt.IMG_420)
                                        {
                                            enc.Subsample = SixLabors.ImageSharp.Formats.Jpeg.JpegSubsample.Ratio420;
                                        }
                                        image.SaveAsJpeg(outStream, enc);
                                    }

                                    // Use MozJpeg
                                    if (true)
                                    {
                                        var bmpenc = new SixLabors.ImageSharp.Formats.Bmp.BmpEncoder();
                                        image.Save(outStream, bmpenc);
                                        outStream.Seek(0, SeekOrigin.Begin);

                                        var tjc = new MozJpegSharp.TJCompressor();
                                        var b   = new System.Drawing.Bitmap(outStream);
                                        //var compressed = tjc.Compress(b, MozJpegSharp.TJSubsamplingOption.Chrominance420, 75, MozJpegSharp.TJFlags.None);
                                        var compressed = tjc.Compress(b, MozJpegSharp.TJSubsamplingOption.Chrominance420, 30, MozJpegSharp.TJFlags.None);
                                        outStream      = new MemoryStream(compressed);
                                    }
                                }
                                else
                                {
                                    image.SaveAsJpeg(outStream);
                                }
                            }
#endif

                            outStream.Seek(0, SeekOrigin.Begin);
                            zipWriter.Write(item.Key, outStream);

                            ProgressValue++;
                        }
                    }

                IsEnableProgressClose = true;
                return(1);
            });
        }