Exemplo n.º 1
0
        /// <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
            {
                m_compressionInfo      = new CompressionInfo();
                m_compressionInfo.Type = DEFAULT_COMPRESSION_METHOD;
                m_compressionInfo.DeflateCompressionLevel = DEFAULT_COMPRESSION_LEVEL;

                string          cpmethod;
                CompressionType tmptype;
                if (options.TryGetValue(COMPRESSION_METHOD_OPTION, out cpmethod) && Enum.TryParse <SharpCompress.Common.CompressionType>(cpmethod, true, out tmptype))
                {
                    m_compressionInfo.Type = tmptype;
                }

                string cplvl;
                int    tmplvl;
                if (options.TryGetValue(COMPRESSION_LEVEL_OPTION, out cplvl) && int.TryParse(cplvl, out tmplvl))
                {
                    m_compressionInfo.DeflateCompressionLevel = (SharpCompress.Compressor.Deflate.CompressionLevel)Math.Max(Math.Min(9, tmplvl), 0);
                }
                else if (options.TryGetValue(COMPRESSION_LEVEL_OPTION_ALIAS, out cplvl) && int.TryParse(cplvl, out tmplvl))
                {
                    m_compressionInfo.DeflateCompressionLevel = (SharpCompress.Compressor.Deflate.CompressionLevel)Math.Max(Math.Min(9, tmplvl), 0);
                }

                m_defaultCompressionLevel = m_compressionInfo.DeflateCompressionLevel;

                m_isWriting = true;
                m_stream    = new System.IO.FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Read);
                m_writer    = WriterFactory.Open(m_stream, ArchiveType.Zip, m_compressionInfo);

                //Size of endheader, taken from SharpCompress ZipWriter
                m_flushBufferSize = 8 + 2 + 2 + 4 + 4 + 2 + 0;
            }
        }
Exemplo n.º 2
0
        /// <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
            {
                m_compressionInfo = new CompressionInfo();
                m_compressionInfo.Type = DEFAULT_COMPRESSION_METHOD;
                m_compressionInfo.DeflateCompressionLevel = DEFAULT_COMPRESSION_LEVEL;

                string cpmethod;
                CompressionType tmptype;
                if (options.TryGetValue(COMPRESSION_METHOD_OPTION, out cpmethod) && Enum.TryParse<SharpCompress.Common.CompressionType>(cpmethod, true, out tmptype))
                    m_compressionInfo.Type = tmptype;

                string cplvl;
                int tmplvl;
                if (options.TryGetValue(COMPRESSION_LEVEL_OPTION, out cplvl) && int.TryParse(cplvl, out tmplvl))
                    m_compressionInfo.DeflateCompressionLevel = (SharpCompress.Compressor.Deflate.CompressionLevel)Math.Max(Math.Min(9, tmplvl), 0);
                else if (options.TryGetValue(COMPRESSION_LEVEL_OPTION_ALIAS, out cplvl) && int.TryParse(cplvl, out tmplvl))
                    m_compressionInfo.DeflateCompressionLevel = (SharpCompress.Compressor.Deflate.CompressionLevel)Math.Max(Math.Min(9, tmplvl), 0);

                m_defaultCompressionLevel = m_compressionInfo.DeflateCompressionLevel;

                m_isWriting = true;
                m_stream = new System.IO.FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Read);
                m_writer = WriterFactory.Open(m_stream, ArchiveType.Zip, m_compressionInfo);

                //Size of endheader, taken from SharpCompress ZipWriter
                m_flushBufferSize = 8 + 2 + 2 + 4 + 4 + 2 + 0;
            }
        }