Beispiel #1
0
        public static void Zip7(string filename)
        {
            var encoder        = new SevenZip.SDK.Compress.LZMA.Encoder();
            var outputFilename = filename + "zipped.7z";

            using (var input = new FileStream(filename, FileMode.Open))
            {
                using (var output = new FileStream(outputFilename, FileMode.Create))
                {
                    encoder.Code(input, output, input.Length, -1, null);
                    output.Flush();
                }
            }
        }
Beispiel #2
0
        private static void CompressFile(string inFile, string outFile)
        {
            var coder  = new SevenZip.SDK.Compress.LZMA.Encoder();
            var input  = new FileStream(inFile, FileMode.Open);
            var output = new FileStream(outFile, FileMode.Create);

            coder.WriteCoderProperties(output);
            output.Write(BitConverter.GetBytes(input.Length), 0, 8);

            coder.Code(input, output, input.Length, -1, null);
            output.Flush();
            output.Close();
            input.Close();
        }
Beispiel #3
0
        public byte[] CompressFile(UserGet chunk)
        {
            Int32 dictionary     = 1 << 23;
            Int32 posStateBits   = 2;
            Int32 litContextBits = 3; // for normal files
            // UInt32 litContextBits = 0; // for 32-bit data
            Int32 litPosBits = 0;
            // UInt32 litPosBits = 2; // for 32-bit data
            Int32 algorithm    = 2;
            Int32 numFastBytes = 128;

            string mf        = "bt4";
            bool   eos       = true;
            bool   stdInMode = false;

            CoderPropID[] propIDs =
            {
                CoderPropID.DictionarySize,
                CoderPropID.PosStateBits,
                CoderPropID.LitContextBits,
                CoderPropID.LitPosBits,
                CoderPropID.Algorithm,
                CoderPropID.NumFastBytes,
                CoderPropID.MatchFinder,
                CoderPropID.EndMarker
            };

            object[] properties =
            {
                (Int32)(dictionary),
                (Int32)(posStateBits),
                (Int32)(litContextBits),
                (Int32)(litPosBits),
                (Int32)(algorithm),
                (Int32)(numFastBytes),
                mf,
                eos
            };

            using (var tg = new MemoryStream())
                using (var outStream = new MemoryStream())
                {
                    Serializer.Serialize(tg, chunk);
                    tg.Position = 0;
                    var encoder = new SevenZip.SDK.Compress.LZMA.Encoder();
                    encoder.SetCoderProperties(propIDs, properties);
                    encoder.WriteCoderProperties(outStream);
                    Int64 fileSize;
                    if (eos || stdInMode)
                    {
                        fileSize = -1;
                    }
                    else
                    {
                        fileSize = tg.Length;
                    }
                    for (int i = 0; i < 8; i++)
                    {
                        outStream.WriteByte((Byte)(fileSize >> (8 * i)));
                    }
                    encoder.Code(tg, outStream, -1, -1, null);
                    return(outStream.ToArray());
                }
        }
Beispiel #4
0
        // This is the compression function
        private static void Compress(string[] args)
        {
            // We call the 7zip compressor
            SevenZip.SDK.Compress.LZMA.Encoder coder = new SevenZip.SDK.Compress.LZMA.Encoder();
            // We get all files in directory list, in an array
            string[] filePaths = Directory.GetFiles(args[0], args[2]);
            // Exist or not, we create the folder
            Directory.CreateDirectory(args[1]);
            // For each files in directory
            foreach (string filePath in filePaths)
            {
                // We get file property ( Size, Owner, Data, ... )
                FileInfo f = new FileInfo(filePath);
                // We work on the inputed files
                using (FileStream input = new FileStream(filePath, FileMode.Open))
                {
                    // We work on the outputed files
                    using (FileStream output = new FileStream(Path.Combine(args[1], Path.GetFileName(filePath)), FileMode.Create))
                    {
                        // byte[] properties = new byte[5] { 0x5d, 0x00, 0x00, 0x04, 0x00};

                        // We create the 7zip property manager array
                        CoderPropID[] propIDs =
                        {
                            CoderPropID.DictionarySize,
                            CoderPropID.PosStateBits,
                            CoderPropID.LitContextBits,
                            CoderPropID.LitPosBits,
                            CoderPropID.Algorithm,
                            CoderPropID.NumFastBytes,
                            CoderPropID.MatchFinder,
                            CoderPropID.EndMarker
                        };

                        Int32 dictionary     = 1 << 18;
                        Int32 posStateBits   = 2; // This is the value for skipping/state
                        Int32 litContextBits = 3; // This is the value for normal files
                        // UInt32 litContextBits = 0; // This is the value for 32-bits files
                        Int32 litPosBits = 0;     // This is the value for offset
                        // UInt32 litPosBits = 2; // This is the value for 32-biys files
                        Int32  algorithm    = 2;  // This is the algorithm for compression/decompression, LZMA
                        Int32  numFastBytes = 32; // Unknown
                        string mf           = "bt4";
                        bool   eos          = false;

                        // We store as an object
                        object[] properties =
                        {
                            (Int32)(dictionary),
                            (Int32)(posStateBits),
                            (Int32)(litContextBits),
                            (Int32)(litPosBits),
                            (Int32)(algorithm),
                            (Int32)(numFastBytes),
                            mf,
                            eos
                        };

                        // We set the properties in 7-zip properties manager
                        coder.SetCoderProperties(propIDs, properties);
                        // We write the properties in the outputed files
                        coder.WriteCoderProperties(output);
                        // We write the length at the start of files
                        output.Write(BitConverter.GetBytes(input.Length), 0, 4);
                        // We write the compressed data in the files
                        coder.Code(input, output, input.Length, -1, null);
                        // Free
                        output.Flush();
                        // Free
                        output.Close();
                    }
                    // Free
                    input.Close();
                }
            }
        }