Beispiel #1
0
    public static byte[] Decompress(byte[] inBytes, string outPath = "", SevenZip.ICodeProgress progress = null)
    {
        byte[] nbytes    = null;
        Stream inStream  = new MemoryStream(inBytes);
        Stream outStream = null;

        if (string.IsNullOrEmpty(outPath))
        {
            outStream = new MemoryStream();
        }
        else
        {
            outStream = new FileStream(outPath, FileMode.Create, FileAccess.ReadWrite);
        }
        BinaryReader sr = new BinaryReader(inStream);

        if (inStream != null && outStream != null)
        {
            byte[] properties = new byte[5];
            if (sr.Read(properties, 0, 5) != 5)
            {
                throw (new Exception("input .lzma is too short"));
            }
            //inStream.Seek(5, SeekOrigin.Begin);

            SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
            decoder.SetDecoderProperties(properties);
            long outSize = 0;
            for (int i = 0; i < 8; i++)
            {
                int v = inStream.ReadByte();
                if (v < 0)
                {
                    throw (new Exception("Can't Read 1"));
                }
                outSize |= ((long)(byte)v) << (8 * i);
            }
            long compressedSize = inStream.Length - inStream.Position;
            mLength = compressedSize;
            decoder.Code(inStream, outStream, compressedSize, outSize, progress);

            sr.Close();
            inStream.Close();

            outStream.Position = 0;//Seek(0, SeekOrigin.Begin);
            sr     = new BinaryReader(outStream);
            nbytes = sr.ReadBytes((int)outStream.Length);
            sr.Close();
            outStream.Close();
        }
        return(nbytes);
    }
Beispiel #2
0
 /// <summary>
 /// 同步压缩
 /// </summary>
 /// <param name="inFile"></param>
 /// <param name="outFile"></param>
 /// <param name="progress"></param>
 public static void Compress(string inFile, string outFile, SevenZip.ICodeProgress progress = null)
 {
     SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder();
     using (var input = File.OpenRead(inFile))
     {
         using (var output = File.Create(outFile))
         {
             coder.WriteCoderProperties(output);
             output.Write(System.BitConverter.GetBytes(input.Length), 0, 8);
             coder.Code(input, output, input.Length, -1, progress);
         }
     }
 }
Beispiel #3
0
    public static byte[] Compress(byte[] inBytes, string outPath = "", SevenZip.ICodeProgress progress = null)
    {
        byte[] nbytes    = null;
        Stream inStream  = new MemoryStream(inBytes);
        Stream outStream = null;

        if (string.IsNullOrEmpty(outPath))
        {
            outStream = new MemoryStream();
        }
        else
        {
            outStream = new FileStream(outPath, FileMode.Create, FileAccess.ReadWrite);
        }
        if (inStream != null && outStream != null)
        {
            SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();
            encoder.SetCoderProperties(propIDs, properties);
            encoder.WriteCoderProperties(outStream);
            Int64 fileSize;
            if (eos || stdInMode)
            {
                fileSize = -1;
            }
            else
            {
                fileSize = inStream.Length;
            }
            for (int i = 0; i < 8; i++)
            {
                outStream.WriteByte((Byte)(fileSize >> (8 * i)));
            }
            mLength = inStream.Length;
            encoder.Code(inStream, outStream, -1, -1, progress);

            inStream.Close();

            outStream.Position = 0;//Seek(0, SeekOrigin.Begin);
            BinaryReader sr = new BinaryReader(outStream);
            nbytes = sr.ReadBytes((int)outStream.Length);
            sr.Close();
            outStream.Close();
        }
        return(nbytes);
    }
Beispiel #4
0
 public static void Decompress(string inFile, string outFile, SevenZip.ICodeProgress progress = null)
 {
     SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
     using (var input = File.Create(inFile))
     {
         using (var output = File.Create(outFile))
         {
             int    propertiesSize = SevenZip.Compression.LZMA.Encoder.kPropSize;
             byte[] properties     = new byte[propertiesSize];
             input.Read(properties, 0, propertiesSize);
             byte[] fileLengthBytes = new byte[8];
             input.Read(fileLengthBytes, 0, 8);
             long fileLength = System.BitConverter.ToInt64(fileLengthBytes, 0);
             decoder.SetDecoderProperties(properties);
             decoder.Code(input, output, input.Length, fileLength, progress);
         }
     }
 }
Beispiel #5
0
 public static void Decompress(byte[] inFileByteArray, string outFile, SevenZip.ICodeProgress progress = null, System.Action codeComplete = null)
 {
     SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
     using (var input = new MemoryStream(inFileByteArray))
     {
         using (var output = File.Create(outFile))
         {
             byte[] properties = new byte[5];
             input.Read(properties, 0, 5);
             byte[] fileLengthBytes = new byte[8];
             input.Read(fileLengthBytes, 0, 8);
             long fileLength = System.BitConverter.ToInt64(fileLengthBytes, 0);
             decoder.SetDecoderProperties(properties);
             decoder.Code(input, output, input.Length, fileLength, progress);
         }
     }
     if (codeComplete != null)
     {
         codeComplete();
     }
 }