Beispiel #1
0
 public ZipFile(Stream stream)
 {
     useZip64_ = UseZip64.Dynamic;
     bufferSize_ = DefaultBufferSize;
     updateEntryFactory_ = new ZipEntryFactory();
     if (stream == null)
     {
         throw new ArgumentNullException("stream");
     }
     if (!stream.CanSeek)
     {
         throw new ArgumentException("Stream is not seekable", "stream");
     }
     baseStream_ = stream;
     isStreamOwner = true;
     if (baseStream_.Length > 0L)
     {
         try
         {
             ReadEntries();
             return;
         }
         catch
         {
             DisposeInternal(true);
             throw;
         }
     }
     entries_ = new ZipEntry[0];
     isNewArchive_ = true;
 }
Beispiel #2
0
        public void ZipOutputStreamEncryptEmptyEntries(
            [Values] UseZip64 useZip64,
            [Values(0, 128, 256)] int keySize,
            [Values(CompressionMethod.Stored, CompressionMethod.Deflated)] CompressionMethod compressionMethod)
        {
            using (var ms = new MemoryStream())
            {
                using (var zipOutputStream = new ZipOutputStream(ms))
                {
                    zipOutputStream.IsStreamOwner = false;
                    zipOutputStream.Password      = "******";
                    zipOutputStream.UseZip64      = useZip64;

                    ZipEntry zipEntry = new ZipEntry("emptyEntry")
                    {
                        AESKeySize        = keySize,
                        CompressionMethod = compressionMethod,
                        CompressedSize    = 0,
                        Crc  = 0,
                        Size = 0,
                    };

                    zipOutputStream.PutNextEntry(zipEntry);
                    zipOutputStream.CloseEntry();
                }

                SevenZipHelper.VerifyZipWith7Zip(ms, "password");
            }
        }
Beispiel #3
0
 public ZipFile(Stream stream)
 {
     _useZip64 = UseZip64.Dynamic;
     _bufferSize = DefaultBufferSize;
     _updateEntryFactory = new ZipEntryFactory();
     if (stream == null)
     {
         throw new ArgumentNullException(nameof(stream));
     }
     if (!stream.CanSeek)
     {
         throw new ArgumentException("Stream is not seekable", nameof(stream));
     }
     _baseStream = stream;
     _isStreamOwner = true;
     if (_baseStream.Length > 0L)
     {
         try
         {
             ReadEntries();
             return;
         }
         catch
         {
             DisposeInternal(true);
             throw;
         }
     }
     _entries = new ZipEntry[0];
     _isNewArchive = true;
 }
Beispiel #4
0
 public ZipFile(FileStream file)
 {
     _useZip64 = UseZip64.Dynamic;
     _bufferSize = DefaultBufferSize;
     _updateEntryFactory = new ZipEntryFactory();
     if (file == null)
     {
         throw new ArgumentNullException(nameof(file));
     }
     if (!file.CanSeek)
     {
         throw new ArgumentException("Stream is not seekable", nameof(file));
     }
     _baseStream = file;
     _name = file.Name;
     _isStreamOwner = true;
     try
     {
         ReadEntries();
     }
     catch
     {
         DisposeInternal(true);
         throw;
     }
 }
Beispiel #5
0
 public ZipFile(FileStream file)
 {
     useZip64_ = UseZip64.Dynamic;
     bufferSize_ = DefaultBufferSize;
     updateEntryFactory_ = new ZipEntryFactory();
     if (file == null)
     {
         throw new ArgumentNullException("file");
     }
     if (!file.CanSeek)
     {
         throw new ArgumentException("Stream is not seekable", "file");
     }
     baseStream_ = file;
     name_ = file.Name;
     isStreamOwner = true;
     try
     {
         ReadEntries();
     }
     catch
     {
         DisposeInternal(true);
         throw;
     }
 }
Beispiel #6
0
 internal ZipFile()
 {
     _useZip64 = UseZip64.Dynamic;
     _bufferSize = DefaultBufferSize;
     _updateEntryFactory = new ZipEntryFactory();
     _entries = new ZipEntry[0];
     _isNewArchive = true;
 }
Beispiel #7
0
 public ZipOutputStream(Stream baseOutputStream, int bufferSize)
     : base(baseOutputStream, new Deflater(-1, true), bufferSize)
 {
     _entries = new ArrayList();
     _crc = new Crc32();
     _defaultCompressionLevel = -1;
     _curMethod = CompressionMethod.Deflated;
     _zipComment = new byte[0];
     _crcPatchPos = -1L;
     _sizePatchPos = -1L;
     _useZip64 = UseZip64.Dynamic;
 }
Beispiel #8
0
 public ZipOutputStream(Stream baseOutputStream, int bufferSize)
     : base(baseOutputStream, new Deflater(-1, true), bufferSize)
 {
     _entries = new ArrayList();
     _crc     = new Crc32();
     _defaultCompressionLevel = -1;
     _curMethod    = CompressionMethod.Deflated;
     _zipComment   = new byte[0];
     _crcPatchPos  = -1L;
     _sizePatchPos = -1L;
     _useZip64     = UseZip64.Dynamic;
 }
Beispiel #9
0
 public ZipOutputStream(Stream baseOutputStream)
     : base(baseOutputStream, new Deflater(-1, true))
 {
     entries = new ArrayList();
     crc = new Crc32();
     defaultCompressionLevel = -1;
     curMethod = CompressionMethod.Deflated;
     zipComment = new byte[0];
     crcPatchPos = -1L;
     sizePatchPos = -1L;
     useZip64_ = UseZip64.Dynamic;
 }
Beispiel #10
0
 public FastZip(FastZipEvents events)
 {
     _entryFactory = new ZipEntryFactory();
     _useZip64     = UseZip64.Dynamic;
     _events       = events;
 }
Beispiel #11
0
 public FastZip()
 {
     _entryFactory = new ZipEntryFactory();
     _useZip64     = UseZip64.Dynamic;
 }
        /// <summary>
        /// Parse command line arguments.
        /// This is fairly flexible without using any custom classes.  Arguments and options can appear
        /// in any order and are case insensitive.  Arguments for options are signalled with an '='
        /// as in -demo=argument, sometimes the '=' can be omitted as well secretly.
        /// Grouping of single character options is supported.
        /// </summary>		
        /// <returns>
        /// true if arguments are valid such that processing should continue
        /// </returns>
        bool SetArgs(string[] args)
        {
            bool result = true;
            int argIndex = 0;

            while (argIndex < args.Length)
            {
                if (args[argIndex][0] == '-' || args[argIndex][0] == '/')
                {

                    string option = args[argIndex].Substring(1).ToLower();
                    string optArg = "";

                    int parameterIndex = option.IndexOf('=');

                    if (parameterIndex >= 0)
                    {
                        if (parameterIndex < option.Length - 1)
                        {
                            optArg = option.Substring(parameterIndex + 1);
                        }
                        option = option.Substring(0, parameterIndex);
                    }

            #if OPTIONTEST
                    Console.WriteLine("args index [{0}] option [{1}] argument [{2}]", argIndex, option, optArg);
            #endif
                    if (option.Length == 0)
                    {
                        System.Console.Error.WriteLine("Invalid argument (0}", args[argIndex]);
                        result = false;
                    }
                    else
                    {
                        int optionIndex = 0;
                        while (optionIndex < option.Length)
                        {
            #if OPTIONTEST
                            Console.WriteLine("optionIndex {0}", optionIndex);
            #endif
                            switch(option[optionIndex])
                            {
                                case '-': // long option
                                    optionIndex = option.Length;

                                    switch (option)
                                    {
                                        case "-add":
                                            operation_ = Operation.Add;
                                            break;

                                        case "-create":
                                            operation_ = Operation.Create;
                                            break;

                                        case "-list":
                                            operation_ = Operation.List;
                                            break;

                                        case "-extract":
                                            operation_ = Operation.Extract;
                                            if (optArg.Length > 0)
                                            {
                                                targetOutputDirectory_ = optArg;
                                            }
                                            break;

                                        case "-delete":
                                            operation_ = Operation.Delete;
                                            break;

                                        case "-test":
                                            operation_ = Operation.Test;
                                            break;

                                        case "-env":
                                            ShowEnvironment();
                                            break;

                                        case "-emptydirs":
                                            addEmptyDirectoryEntries_ = true;
                                            break;

                                        case "-data":
                                            testData_ = true;
                                            break;

                                        case "-zip64":
                                            if ( optArg.Length > 0 )
                                            {
                                                switch ( optArg )
                                                {
                                                    case "on":
                                                        useZip64_ = UseZip64.On;
                                                        break;

                                                    case "off":
                                                        useZip64_ = UseZip64.Off;
                                                        break;

                                                    case "auto":
                                                        useZip64_ = UseZip64.Dynamic;
                                                        break;
                                                }
                                            }
                                            break;

                                        case "-encoding":
                                            if (optArg.Length > 0)
                                            {
                                                if (IsNumeric(optArg))
                                                {
                                                    try
                                                    {
                                                        int enc = int.Parse(optArg);
                                                        if (Encoding.GetEncoding(enc) != null)
                                                        {
            #if OPTIONTEST
                                                            Console.WriteLine("Encoding set to {0}", enc);
            #endif
                                                            ZipConstants.DefaultCodePage = enc;
                                                        }
                                                        else
                                                        {
                                                            result = false;
                                                            System.Console.Error.WriteLine("Invalid encoding " + args[argIndex]);
                                                        }
                                                    }
                                                    catch (Exception)
                                                    {
                                                        result = false;
                                                        System.Console.Error.WriteLine("Invalid encoding " + args[argIndex]);
                                                    }
                                                }
                                                else
                                                {
                                                    try
                                                    {
                                                        ZipConstants.DefaultCodePage = Encoding.GetEncoding(optArg).CodePage;
                                                    }
                                                    catch (Exception)
                                                    {
                                                        result = false;
                                                        System.Console.Error.WriteLine("Invalid encoding " + args[argIndex]);
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                result = false;
                                                System.Console.Error.WriteLine("Missing encoding parameter");
                                            }
                                            break;

                                        case "-version":
                                            ShowVersion();
                                            break;

                                        case "-help":
                                            ShowHelp();
                                            break;

                                        case "-restore-dates":
                                            restoreDateTime_ = true;
                                            break;

                                        default:
                                            System.Console.Error.WriteLine("Invalid long argument " + args[argIndex]);
                                            result = false;
                                            break;
                                    }
                                    break;

                                case '?':
                                    ShowHelp();
                                    break;

                                case 's':
                                    if (optionIndex != 0)
                                    {
                                        result = false;
                                        System.Console.Error.WriteLine("-s cannot be in a group");
                                    }
                                    else
                                    {
                                        if (optArg.Length > 0)
                                        {
                                            password_ = optArg;
                                        }
                                        else if (option.Length > 1)
                                        {
                                            password_ = option.Substring(1);
                                        }
                                        else
                                        {
                                            System.Console.Error.WriteLine("Missing argument to " + args[argIndex]);
                                        }
                                    }
                                    optionIndex = option.Length;
                                    break;

                                case 't':
                                    operation_ = Operation.Test;
                                    break;

                                case 'c':
                                    operation_ = Operation.Create;
                                    break;

                                case 'e':
                                    if (optionIndex != 0)
                                    {
                                        result = false;
                                        System.Console.Error.WriteLine("-e cannot be in a group");
                                    }
                                    else
                                    {
                                        optionIndex = option.Length;
                                        if (optArg.Length > 0)
                                        {
                                            try
                                            {
                                                compressionLevel_ = int.Parse(optArg);
                                            }
                                            catch (Exception)
                                            {
                                                System.Console.Error.WriteLine("Level invalid");
                                            }
                                        }
                                    }
                                    optionIndex = option.Length;
                                    break;

                                case 'o':
                                    optionIndex += 1;
                                    overwriteFiles = optionIndex < option.Length ? (option[optionIndex] == '+') ? Overwrite.Always : Overwrite.Never : Overwrite.Never;
                                    break;

                                case 'q':
                                    silent_ = true;
                                    if (overwriteFiles == Overwrite.Prompt)
                                    {
                                        overwriteFiles = Overwrite.Never;
                                    }
                                    break;

                                case 'r':
                                    recursive_ = true;
                                    break;

                                case 'v':
                                    operation_ = Operation.List;
                                    break;

                                case 'x':
                                    if (optionIndex != 0)
                                    {
                                        result = false;
                                        System.Console.Error.WriteLine("-x cannot be in a group");
                                    }
                                    else
                                    {
                                        operation_ = Operation.Extract;
                                        if (optArg.Length > 0)
                                        {
                                            targetOutputDirectory_ = optArg;
                                        }
                                    }
                                    optionIndex = option.Length;
                                    break;

                                default:
                                    System.Console.Error.WriteLine("Invalid argument: " + args[argIndex]);
                                    result = false;
                                    break;
                            }
                            ++optionIndex;
                        }
                    }
                }
                else
                {
            #if OPTIONTEST
                    Console.WriteLine("file spec {0} = '{1}'", argIndex, args[argIndex]);
            #endif
                    fileSpecs_.Add(args[argIndex]);
                }
                ++argIndex;
            }

            if (fileSpecs_.Count > 0)
            {
                string checkPath = (string)fileSpecs_[0];
                int deviceCheck = checkPath.IndexOf(':');
            #if NET_VER_1
                if (checkPath.IndexOfAny(Path.InvalidPathChars) >= 0
            #else
                if (checkPath.IndexOfAny(Path.GetInvalidPathChars()) >= 0
            #endif
                    || checkPath.IndexOf('*') >= 0 || checkPath.IndexOf('?') >= 0
                    || ((deviceCheck >= 0) && (deviceCheck != 1)))
                {
                    Console.WriteLine("There are invalid characters in the specified zip file name");
                    result = false;
                }
            }
            return result && (fileSpecs_.Count > 0);
        }
Beispiel #13
0
 public ZipFile(string name)
 {
     useZip64_ = UseZip64.Dynamic;
     bufferSize_ = DefaultBufferSize;
     updateEntryFactory_ = new ZipEntryFactory();
     if (name == null)
     {
         throw new ArgumentNullException("name");
     }
     name_ = name;
     baseStream_ = File.Open(name, FileMode.Open, FileAccess.Read, FileShare.Read);
     isStreamOwner = true;
     try
     {
         ReadEntries();
     }
     catch
     {
         DisposeInternal(true);
         throw;
     }
 }
Beispiel #14
0
 public FastZip(FastZipEvents events)
 {
     entryFactory_ = new ZipEntryFactory();
     useZip64_ = UseZip64.Dynamic;
     events_ = events;
 }
Beispiel #15
0
 public FastZip()
 {
     entryFactory_ = new ZipEntryFactory();
     useZip64_ = UseZip64.Dynamic;
 }
Beispiel #16
0
 public FastZip()
 {
     _entryFactory = new ZipEntryFactory();
     _useZip64 = UseZip64.Dynamic;
 }
Beispiel #17
0
 public FastZip(FastZipEvents events)
 {
     _entryFactory = new ZipEntryFactory();
     _useZip64 = UseZip64.Dynamic;
     _events = events;
 }
Beispiel #18
0
 internal ZipFile()
 {
     useZip64_ = UseZip64.Dynamic;
     bufferSize_ = DefaultBufferSize;
     updateEntryFactory_ = new ZipEntryFactory();
     entries_ = new ZipEntry[0];
     isNewArchive_ = true;
 }
Beispiel #19
0
        /// <summary>
        /// Parse command line arguments.
        /// This is fairly flexible without using any custom classes.  Arguments and options can appear
        /// in any order and are case insensitive.  Arguments for options are indicated with an '='
        /// as in -demo=argument, sometimes the '=' can be omitted as well secretly.
        /// Grouping of single character options is supported.
        /// 
        /// The actual arguments and their handling is however a grab bag of ad-hoc things and its a bit messy.  Could be a 
        /// bit more rigorous about how things are done.  Up side is almost anything is/can be allowed
        /// </summary>		
        /// <returns>
        /// <c>true</c> if arguments are valid such that processing should continue
        /// </returns>
        bool SetArgs(string[] args)
        {
            bool result = true;
            int argIndex = 0;

            while (argIndex < args.Length) {
                if (args[argIndex][0] == '-' || args[argIndex][0] == '/') {

                    string option = args[argIndex].Substring(1).ToLower();
                    string optArg = "";

                    int parameterIndex = option.IndexOf('=');

                    if (parameterIndex >= 0) {
                        if (parameterIndex < option.Length - 1) {
                            optArg = option.Substring(parameterIndex + 1);
                        }
                        option = option.Substring(0, parameterIndex);
                    }

                    if (option.Length == 0) {
                        Console.WriteLine("Invalid argument {0}", args[argIndex]);
                        result = false;
                    }
                    else {
                        int optionIndex = 0;
                        while (optionIndex < option.Length) {
                            switch(option[optionIndex]) {
                                case '-': // long option
                                    optionIndex = option.Length;

                                    switch (option) {
                                        case "-abs":
                                            relativePathInfo = false;
                                            break;

                                        case "-add":
                                            operation = Operation.Add;
                                            break;

                                        case "-create":
                                            operation = Operation.Create;
                                            break;

                                        case "-list":
                                            operation = Operation.List;
                                            useZipFileWhenListing = true;
                                            break;

                                        case "-extract":
                                            operation = Operation.Extract;
                                            if (optArg.Length > 0) {
                                                targetOutputDirectory = optArg;
                                            }
                                            break;

                                        case "-delete":
                                            operation = Operation.Delete;
                                            break;

                                        case "-test":
                                            operation = Operation.Test;
                                            break;

                                        case "-info":
                                            ShowEnvironment();
                                            break;

                                        case "-emptydirs":
                                            addEmptyDirectoryEntries = true;
                                            break;

                                        case "-data":
                                            testData = true;
                                            break;

                                        case "-extractdir":
                                            if (optArg.Length > 0) {
                                                targetOutputDirectory = optArg;
                                            } else {
                                                result = false;
                                                Console.WriteLine("Invalid extractdir " + args[argIndex]);
                                            }
                                            break;

                                        case "-zip64":
                                            if ( optArg.Length > 0 ) {
                                                switch ( optArg ) {
                                                    case "on":
                                                        useZip64_ = UseZip64.On;
                                                        break;

                                                    case "off":
                                                        useZip64_ = UseZip64.Off;
                                                        break;

                                                    case "auto":
                                                        useZip64_ = UseZip64.Dynamic;
                                                        break;
                                                }
                                            }
                                            break;

                                        case "-encoding":
                                            if (optArg.Length > 0) {
                                                if (IsNumeric(optArg)) {
                                                    try {
                                                        int enc = int.Parse(optArg);
                                                        if (Encoding.GetEncoding(enc) != null) {
                                                            ZipConstants.DefaultCodePage = enc;
                                                        } else {
                                                            result = false;
                                                            Console.WriteLine("Invalid encoding " + args[argIndex]);
                                                        }
                                                    }
                                                    catch (Exception) {
                                                        result = false;
                                                        Console.WriteLine("Invalid encoding " + args[argIndex]);
                                                    }
                                                } else {
                                                    try {
                                                        ZipConstants.DefaultCodePage = Encoding.GetEncoding(optArg).CodePage;
                                                    }
                                                    catch (Exception) {
                                                        result = false;
                                                        Console.WriteLine("Invalid encoding " + args[argIndex]);
                                                    }
                                                }
                                            } else {
                                                result = false;
                                                Console.WriteLine("Missing encoding parameter");
                                            }
                                            break;

                                        case "-store":
                                            useZipStored = true;
                                            break;

                                        case "-deflate":
                                            useZipStored = false;
                                            break;

                                        case "-version":
                                            ShowVersion();
                                            break;

                                        case "-help":
                                            ShowHelp();
                                            break;
            #if !NETCF
                                        case "-restore-dates":
                                            restoreDateTime = true;
                                            break;
            #endif

                                        default:
                                            Console.WriteLine("Invalid long argument " + args[argIndex]);
                                            result = false;
                                            break;
                                    }
                                    break;

                                case '?':
                                    ShowHelp();
                                    break;

                                case 's':
                                    if (optionIndex != 0) {
                                        result = false;
                                        Console.WriteLine("-s cannot be in a group");
                                    } else {
                                        if (optArg.Length > 0) {
                                            password = optArg;
                                        } else if (option.Length > 1) {
                                            password = option.Substring(1);
                                        } else {
                                            Console.WriteLine("Missing argument to " + args[argIndex]);
                                        }
                                    }
                                    optionIndex = option.Length;
                                    break;

                                case 'c':
                                    operation = Operation.Create;
                                    break;

                                case 'l':
                                    if (optionIndex != 0) {
                                        result = false;
                                        Console.WriteLine("-l cannot be in a group");
                                    } else {
                                        if (optArg.Length > 0) {
                                            try {
                                                compressionLevel = int.Parse(optArg);
                                            }
                                            catch (Exception) {
                                                Console.WriteLine("Level invalid");
                                            }
                                        }
                                    }
                                    optionIndex = option.Length;
                                    break;

                                case 'o':
                                    optionIndex += 1;
                                    overwriteFiles = (optionIndex < option.Length) ? (option[optionIndex] == '+') ? Overwrite.Always : Overwrite.Never : Overwrite.Never;
                                    break;

                                case 'p':
                                    relativePathInfo = true;
                                    break;

                                case 'q':
                                    silent = true;
                                    if (overwriteFiles == Overwrite.Prompt) {
                                        overwriteFiles = Overwrite.Never;
                                    }
                                    break;

                                case 'r':
                                    recursive = true;
                                    break;

                                case 'v':
                                    operation = Operation.List;
                                    break;

                                case 'x':
                                    if (optionIndex != 0) {
                                        result = false;
                                        Console.WriteLine("-x cannot be in a group");
                                    } else {
                                        operation = Operation.Extract;
                                        if (optArg.Length > 0) {
                                            targetOutputDirectory = optArg;
                                        }
                                    }
                                    optionIndex = option.Length;
                                    break;

                                default:
                                    Console.WriteLine("Invalid argument: " + args[argIndex]);
                                    result = false;
                                    break;
                            }
                            ++optionIndex;
                        }
                    }
                }
                else {
                    fileSpecs.Add(args[argIndex]);
                }
                ++argIndex;
            }

            if (fileSpecs.Count > 0 && operation == Operation.Create) {
                var checkPath = (string)fileSpecs[0];
                int deviceCheck = checkPath.IndexOf(':');
            #if NETCF_1_0
                if (checkPath.IndexOfAny(Path.InvalidPathChars) >= 0
            #else
                if (checkPath.IndexOfAny(Path.GetInvalidPathChars()) >= 0
            #endif
                    || checkPath.IndexOf('*') >= 0 || checkPath.IndexOf('?') >= 0
                    || (deviceCheck >= 0 && deviceCheck != 1)) {
                        Console.WriteLine("There are invalid characters in the specified zip file name");
                        result = false;
                    }
            }
            return result && (fileSpecs.Count > 0);
        }
Beispiel #20
0
 public ZipFile(string name)
 {
     _useZip64 = UseZip64.Dynamic;
     _bufferSize = DefaultBufferSize;
     _updateEntryFactory = new ZipEntryFactory();
     if (name == null)
     {
         throw new ArgumentNullException(nameof(name));
     }
     _name = name;
     _baseStream = File.Open(name, FileMode.Open, FileAccess.Read, FileShare.Read);
     _isStreamOwner = true;
     try
     {
         ReadEntries();
     }
     catch
     {
         DisposeInternal(true);
         throw;
     }
 }