Exemplo n.º 1
0
        /// <summary>
        /// Gzip壓縮
        /// </summary>
        public static int GzipCompress(ref byte[] srcBuffer, ref byte[] cmpBuffer)
        {
            string _CompressSaveFilePath = null;

            try
            {
                string directoryName = "";
                if (OSType != 0)
                {
                    directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Remove(0, 6) + "\\Compress\\";
                }
                else
                {
                    directoryName = "./" + "Compress/";
                }

                DirectoryInfo Createdir = new DirectoryInfo(directoryName);
                if (!Createdir.Exists)
                {
                    Createdir.Create();
                }

                _CompressSaveFilePath = directoryName + RandomSession.createSession(64);

                using (FileStream fs_saveCompress = new FileStream(_CompressSaveFilePath, FileMode.Create, FileAccess.Write))
                {
                    using (GZipStream ZipStream = new GZipStream(fs_saveCompress, CompressionMode.Compress, true))
                    {
                        ZipStream.Write(srcBuffer, 0, srcBuffer.Length);
                    }
                }

                using (FileStream fs_open = new FileStream(_CompressSaveFilePath, FileMode.Open, FileAccess.Read))
                {
                    cmpBuffer = new byte[Convert.ToInt32(fs_open.Length)];
                    fs_open.Read(cmpBuffer, 0, Convert.ToInt32(fs_open.Length));
                }

                return(1);
            }
            catch (Exception ex)
            {
                KConsole.Write(ErrorLevel.Serious, "Kernel>>Tools>>GzipCompress>>", ex.Message);
                return(0);
            }
            finally
            {
                DiskIO.Del(_CompressSaveFilePath);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gzip 解壓縮
        /// </summary>
        public static int Decompress(byte[] _srcBuffer, ref byte[] deCmpBuffer)
        {
            string _DecompressSaveFilePath = null;

            try
            {
                string directoryName = "";
                if (OSType != 0)
                {
                    directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Remove(0, 6) + "\\Compress\\";
                }
                else
                {
                    directoryName = "./" + "Compress/";
                }


                DirectoryInfo Createdir = new DirectoryInfo(directoryName);
                if (!Createdir.Exists)
                {
                    Createdir.Create();
                }

                _DecompressSaveFilePath = directoryName + RandomSession.createSession(64);


                using (MemoryStream ms_open = new MemoryStream(_srcBuffer))
                {
                    using (GZipStream zipStream = new GZipStream(ms_open, CompressionMode.Decompress))
                    {
                        using (FileStream fs_saveDecompress = new FileStream(_DecompressSaveFilePath, FileMode.Create, FileAccess.Write, FileShare.Write))
                        {
                            using (BinaryWriter bw = new BinaryWriter(fs_saveDecompress))
                            {
                                int    offset     = 0;
                                int    BufferSize = 512;
                                int    bytesRead  = 0;
                                byte[] buffer     = new byte[BufferSize];
                                while (true)
                                {
                                    bytesRead = zipStream.Read(buffer, offset, BufferSize);
                                    if (bytesRead == 0)
                                    {
                                        break;
                                    }
                                    bw.Write(buffer, 0, bytesRead);
                                }
                            }
                        }


                        using (FileStream fs_open = new FileStream(_DecompressSaveFilePath, FileMode.Open, FileAccess.Read))
                        {
                            deCmpBuffer = new byte[Convert.ToInt32(fs_open.Length)];
                            fs_open.Read(deCmpBuffer, 0, Convert.ToInt32(fs_open.Length));
                        }
                    }
                }
                return(1);
            }
            catch (Exception ex)
            {
                ///KConsole.Write(ErrorLevel.Serious, "Kernel>>Tools>>Decompress>>", ex.Message);
                Console.Write("Kernel>>Tools>>Decompress>>" + ex.Message);
                return(0);
            }
            finally
            {
                DiskIO.Del(_DecompressSaveFilePath);
            }
        }