public static byte[] Decompress(byte[] b)
 {
     using (var decompressor = new ZstdNet.Decompressor())
     {
         return(decompressor.Unwrap(b));
     }
 }
 public static byte[] Decompress(byte[] b, int MaxDecompressedSize)
 {
     using (var decompressor = new ZstdNet.Decompressor())
     {
         return(decompressor.Unwrap(b, MaxDecompressedSize));
     }
 }
Ejemplo n.º 3
0
        protected override Stream Decompress(Stream uncompressed)
        {
            var header = new byte[9];
            int read   = 0;

            do
            {
                read += uncompressed.Read(header, read, header.Length - read);
            } while (read < header.Length);
            if (header[0] != Header[0])
            {
                throw new FormatException($"Invalid header value {header[0]}");
            }
            var compressedSize   = BitConverter.ToInt32(header, 1);
            var uncompressedSize = BitConverter.ToInt32(header, 5);

            read            = 0;
            compressedSize -= header.Length;
            var cdata = new byte[compressedSize];

            do
            {
                read += uncompressed.Read(cdata, read, compressedSize - read);
            } while (read < compressedSize);
            var decomp = new ZstdNet.Decompressor();

            return(new MemoryStream(decomp.Unwrap(cdata, uncompressedSize)));
        }
Ejemplo n.º 4
0
        public Stream GetStream()
        {
            Stream stream = new Substream(
                new FileStream(ArchiveFilename, FileMode.Open, FileAccess.Read, FileShare.Read),
                (long)offset,
                length);

            switch (Compression)
            {
            case ArchiveFileCompression.LZ4:
                return(new LZ4Stream(stream,
                                     LZ4StreamMode.Decompress));

            case ArchiveFileCompression.Zstandard:
                byte[] output;
                using (MemoryStream buffer = new MemoryStream())
                {
                    stream.CopyTo(buffer);
                    output = buffer.ToArray();
                }
                using (ZstdNet.Decompressor zstd = new ZstdNet.Decompressor())
                    return(new MemoryStream(zstd.Unwrap(output), false));    //, (int)_size

            case ArchiveFileCompression.Uncompressed:
                return(stream);

            default:
                throw new InvalidOperationException("Compression type is invalid.");
            }
        }
Ejemplo n.º 5
0
        public static int Dechunk(BinaryWriter writer, BinaryReader reader, int size)
        {
            int uncompressedFileSize = 0;

            using (BinaryReader chunkedFileReader = new BinaryReader(new MemoryStream(reader.ReadBytes(size))))
            {
                while (chunkedFileReader.BaseStream.Position < size)
                {
                    UInt32 uncompressedSize     = chunkedFileReader.ReadUInt32BE();
                    byte   compressionType      = chunkedFileReader.ReadByte();
                    byte   compressionSignature = chunkedFileReader.ReadByte();
                    UInt16 compressedSize       = chunkedFileReader.ReadUInt16BE();

                    if (compressionType == 0x00)
                    {
                        writer.Write(chunkedFileReader.ReadBytes((int)uncompressedSize));
                        uncompressedFileSize += (int)uncompressedSize;
                    }
                    else if (compressionType == 0x0F)
                    {
                        ZstdNet.Decompressor decompressor = new ZstdNet.Decompressor();
                        byte[] input  = chunkedFileReader.ReadBytes(compressedSize);
                        byte[] output = decompressor.Unwrap(input);
                        writer.Write(output);
                        uncompressedFileSize += output.Length;
                    }
                }
            }

            return(uncompressedFileSize);
        }
Ejemplo n.º 6
0
        void OnExtractDownloadFile(List <object> evParams, Action <NotiData> func)
        {
            var dataPath = evParams[0] as string;
            var dataName = evParams[1] as string;
            var buffer   = evParams[2] as byte[];

            try
            {
                using (var decompressor = new ZstdNet.Decompressor())
                {
                    var path  = dataPath + dataName;
                    var bytes = decompressor.Unwrap(buffer);
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                    File.WriteAllBytes(path, bytes);
                    NotiData data = new NotiData(NotiConst.DOWNLOAD_EXTRACT, dataName, path);
                    if (func != null)
                    {
                        func(data);                //回调逻辑层
                    }
                }
            }
            catch (Exception e)
            {
                NotiData data = new NotiData(NotiConst.DOWNLOAD_EXTRACT_FAILED, dataName, e.Message);
                if (func != null)
                {
                    func(data);                //回调逻辑层
                }
            }
        }
Ejemplo n.º 7
0
        internal static byte[] UnpackZstd(byte[] data)
        {
            int unpacked_size = BigEndian.ToInt32(data, 0);

            using (var dec = new ZstdNet.Decompressor())
            {
                var packed = new ArraySegment <byte> (data, 4, data.Length - 4);
                return(dec.Unwrap(packed, unpacked_size));
            }
        }
Ejemplo n.º 8
0
        private static Stream zstdCheckStream(string path)
        {
            Stream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            if (IsZstd(stream))
            {
                var decompressor = new ZstdNet.Decompressor();
                var data         = decompressor.Unwrap(GetBytes(stream));
                stream.Dispose();
                stream = new MemoryStream(data, false);
            }
            return(stream);
        }
Ejemplo n.º 9
0
        private static byte[] Decompress(byte[] compressedBuffer)
        {
            uint compressedMagic = BitConverter.ToUInt32(compressedBuffer, 0);

            Debug.Assert(compressedMagic == 0xFD2FB528);

            byte[] decompressedBuffer = new byte[1024 * 1024];             // 1MB should be enough for anyone!
            int    length             = 0;

            using (ZstdNet.Decompressor dec = new ZstdNet.Decompressor())
            {
                length = dec.Unwrap(compressedBuffer, decompressedBuffer, 0);
            }
            byte[] shrunkBuffer = new byte[length];
            Array.Copy(decompressedBuffer, 0, shrunkBuffer, 0, length);
            return(shrunkBuffer);
        }
Ejemplo n.º 10
0
        public static void ExtractZSToFolder(string path, string dest)
        {
            Console.WriteLine("Dumping zs files...");
            var files = Directory.EnumerateFiles(path, "*.zs", SearchOption.AllDirectories);

            using var zs = new ZstdNet.Decompressor();
            foreach (var f in files)
            {
                var data   = File.ReadAllBytes(f);
                var result = zs.Unwrap(data);

                var rpath = f.Replace(path, dest);
                var dir   = Path.GetDirectoryName(rpath);
                Directory.CreateDirectory(dir);
                File.WriteAllBytes(rpath, result);
                Console.WriteLine(rpath);
            }
        }
Ejemplo n.º 11
0
        static async Task Main(string[] args)
        {
            var pathTest = @"E:\Messageold";

            var sw = new Stopwatch();

            sw.Start();

            var filesIn = Directory.GetFiles(pathTest, "*EUen*.zs");

            var dataOut = new Dictionary <string, byte[]>();


            foreach (var file in filesIn)
            {
                var d = await File.ReadAllBytesAsync(file);

                using var decompress = new ZstdNet.Decompressor();
                var unzip = decompress.Unwrap(d);

                var files = SARC.Extract(unzip);

                foreach (var filem in files)
                {
                    var dat = filem.Value.Sorted.SelectMany(s => Encoding.Unicode.GetBytes($"{s.Key},{s.Value}\r\n")).ToArray();
                    dataOut.Add(file.Replace(pathTest, "") + "\\" + filem.Key, dat);
                }
            }

            await using var zipToOpen = new MemoryStream();
            using var archive         = new ZipArchive(zipToOpen, ZipArchiveMode.Create, true);
            foreach (var file in dataOut)
            {
                var zipArchiveEntry = archive.CreateEntry(file.Key.TrimStart('\\'), CompressionLevel.NoCompression);
                await using var zipStream = zipArchiveEntry.Open();
                await zipStream.WriteAsync(file.Value, 0, file.Value.Length);
            }

            var time = sw.ElapsedMilliseconds;

            await File.WriteAllBytesAsync(@"D:\MessageTest\zip.zip", zipToOpen.ToArray());
        }
Ejemplo n.º 12
0
        public static void Dechunk(string finalName, BinaryReader reader, int size)
        {
            using (BinaryWriter writer = new BinaryWriter(File.Open(finalName, FileMode.Create)))
            {
                using (BinaryReader chunkedFileReader = new BinaryReader(new MemoryStream(reader.ReadBytes(size))))
                {
                    while (chunkedFileReader.BaseStream.Position < size)
                    {
                        UInt32 uncompressedSize     = chunkedFileReader.ReadUInt32BE();
                        byte   compressionType      = chunkedFileReader.ReadByte();
                        byte   compressionSignature = chunkedFileReader.ReadByte();
                        UInt16 compressedSize       = chunkedFileReader.ReadUInt16BE();

                        if (compressionType == 0x00)
                        {
                            writer.Write(chunkedFileReader.ReadBytes((int)uncompressedSize));
                        }
                        else if (compressionType == 0x09)
                        {
                            byte[] input  = chunkedFileReader.ReadBytes(compressedSize);
                            byte[] output = new byte[(int)uncompressedSize];
                            int    result = LZ4Handler.LZ4_decompress_safe(input, output, input.Length, (int)uncompressedSize);

                            if (result != uncompressedSize)
                            {
                                throw new ArgumentException();
                            }

                            writer.Write(output);
                        }
                        else if (compressionType == 0x0F)
                        {
                            ZstdNet.Decompressor decompressor = new ZstdNet.Decompressor();
                            byte[] input  = chunkedFileReader.ReadBytes(compressedSize);
                            byte[] output = decompressor.Unwrap(input);
                            writer.Write(output);
                        }
                    }
                }
            }
        }
Ejemplo n.º 13
0
        public static string ReadFromFile(string filename)
        {
            if (File.Exists(filename))
            {
                return(File.ReadAllText(filename));
            }

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                var decompressor = new ZstdNet.Decompressor();
                return(System.Text.Encoding.UTF8.GetString(decompressor.Unwrap(File.ReadAllBytes(filename + ".zst"))));
            }
            else
            {
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName  = "unzstd";
                startInfo.Arguments = filename + ".zst";
                var process = Process.Start(startInfo);
                process.WaitForExit();

                return(File.ReadAllText(filename));
            }
        }