/// <summary> /// Constructs a new zip instance. /// If the file exists and has a non-zero length we read it, /// otherwise we create a new archive. /// </summary> /// <param name="filename">The name of the file to read or write</param> /// <param name="options">The options passed on the commandline</param> public FileArchiveZip(string filename, Dictionary <string, string> options) { if (string.IsNullOrEmpty(filename) && filename.Trim().Length == 0) { throw new ArgumentException("filename"); } if (File.Exists(filename) && new FileInfo(filename).Length > 0) { m_isWriting = false; m_filename = filename; } else { var compression = new ZipWriterOptions(CompressionType.Deflate); compression.CompressionType = DEFAULT_COMPRESSION_METHOD; compression.DeflateCompressionLevel = DEFAULT_COMPRESSION_LEVEL; m_usingZip64 = compression.UseZip64 = options.ContainsKey(COMPRESSION_ZIP64_OPTION) ? Duplicati.Library.Utility.Utility.ParseBoolOption(options, COMPRESSION_ZIP64_OPTION) : DEFAULT_ZIP64; string cpmethod; CompressionType tmptype; if (options.TryGetValue(COMPRESSION_METHOD_OPTION, out cpmethod) && Enum.TryParse <SharpCompress.Common.CompressionType>(cpmethod, true, out tmptype)) { compression.CompressionType = tmptype; } string cplvl; int tmplvl; if (options.TryGetValue(COMPRESSION_LEVEL_OPTION, out cplvl) && int.TryParse(cplvl, out tmplvl)) { compression.DeflateCompressionLevel = (SharpCompress.Compressors.Deflate.CompressionLevel)Math.Max(Math.Min(9, tmplvl), 0); } else if (options.TryGetValue(COMPRESSION_LEVEL_OPTION_ALIAS, out cplvl) && int.TryParse(cplvl, out tmplvl)) { compression.DeflateCompressionLevel = (SharpCompress.Compressors.Deflate.CompressionLevel)Math.Max(Math.Min(9, tmplvl), 0); } m_defaultCompressionLevel = compression.DeflateCompressionLevel; m_compressionType = compression.CompressionType; m_isWriting = true; m_stream = new System.IO.FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Read); m_writer = WriterFactory.Open(m_stream, ArchiveType.Zip, compression); //Size of endheader, taken from SharpCompress ZipWriter m_flushBufferSize = 8 + 2 + 2 + 4 + 4 + 2 + 0; } }
public ZIP(int level) { if (level < 0) { level = 0; } else if (level > 9) { level = 9; } _compressLevel = (SharpCompress.Compressors.Deflate.CompressionLevel)level; }
/// <summary> /// Constructs a new zip instance. /// Access mode is specified by mode parameter. /// Note that stream would not be disposed by FileArchiveZip instance so /// you may reuse it and have to dispose it yourself. /// </summary> /// <param name="stream">The stream to read or write depending access mode</param> /// <param name="mode">The archive acces mode</param> /// <param name="options">The options passed on the commandline</param> public FileArchiveZip(Stream stream, ArchiveMode mode, IDictionary <string, string> options) { m_stream = stream; m_mode = mode; if (mode == ArchiveMode.Write) { var compression = new ZipWriterOptions(CompressionType.Deflate); compression.CompressionType = DEFAULT_COMPRESSION_METHOD; compression.DeflateCompressionLevel = DEFAULT_COMPRESSION_LEVEL; m_usingZip64 = compression.UseZip64 = options.ContainsKey(COMPRESSION_ZIP64_OPTION) ? Duplicati.Library.Utility.Utility.ParseBoolOption(options, COMPRESSION_ZIP64_OPTION) : DEFAULT_ZIP64; string cpmethod; CompressionType tmptype; if (options.TryGetValue(COMPRESSION_METHOD_OPTION, out cpmethod) && Enum.TryParse <SharpCompress.Common.CompressionType>(cpmethod, true, out tmptype)) { compression.CompressionType = tmptype; } string cplvl; int tmplvl; if (options.TryGetValue(COMPRESSION_LEVEL_OPTION, out cplvl) && int.TryParse(cplvl, out tmplvl)) { compression.DeflateCompressionLevel = (SharpCompress.Compressors.Deflate.CompressionLevel)Math.Max(Math.Min(9, tmplvl), 0); } else if (options.TryGetValue(COMPRESSION_LEVEL_OPTION_ALIAS, out cplvl) && int.TryParse(cplvl, out tmplvl)) { compression.DeflateCompressionLevel = (SharpCompress.Compressors.Deflate.CompressionLevel)Math.Max(Math.Min(9, tmplvl), 0); } m_defaultCompressionLevel = compression.DeflateCompressionLevel; m_compressionType = compression.CompressionType; m_writer = WriterFactory.Open(m_stream, ArchiveType.Zip, compression); //Size of endheader, taken from SharpCompress ZipWriter m_flushBufferSize = 8 + 2 + 2 + 4 + 4 + 2 + 0; } }
public ZIP(CompressionLevel level) { switch (level) { case CompressionLevel.None: _compressLevel = SharpCompress.Compressors.Deflate.CompressionLevel.None; break; case CompressionLevel.Fastest: _compressLevel = SharpCompress.Compressors.Deflate.CompressionLevel.BestSpeed; break; case CompressionLevel.Optimal: _compressLevel = SharpCompress.Compressors.Deflate.CompressionLevel.Level7; break; case CompressionLevel.Slowest: _compressLevel = SharpCompress.Compressors.Deflate.CompressionLevel.BestCompression; break; } }