Beispiel #1
0
 //============================================================
 // <T>压缩数据保存为指定文件。</T>
 //
 // @param fileName 文件名称
 //============================================================
 public void Compress(string fileName)
 {
     // 建立目录
     RDirectory.MakeDirectoriesForFile(fileName);
     // 输出文件
     byte[] data = InnerCompress(_memory, 0, _length);
     using (FByteFile file = new FByteFile()) {
         file.Assign(data, 0, data.Length);
         file.SaveFile(fileName);
     }
 }
Beispiel #2
0
 //============================================================
 // <T>分块压缩保存为字节数组指定文件。</T>
 //
 // @param fileName 文件名称
 // @param blockSize 分块大小
 //============================================================
 public void BlockCompress(string fileName, int blockSize)
 {
     // 检查参数
     if (null == fileName)
     {
         throw new FFatalException("File name is null.");
     }
     // 存储文件
     using (FByteFile file = new FByteFile()) {
         BlockCompress(file, blockSize);
         file.SaveFile(fileName);
     }
 }
Beispiel #3
0
        //============================================================
        // <T>解压缩方法</T>
        //
        // @param fileName 文件名称
        //============================================================
        public void BlockDecompress(string fileName)
        {
            // 检查参数
            if (null == fileName)
            {
                throw new FFatalException("File name is null.");
            }
            FByteFile file = new FByteFile();

            file.LoadFile(fileName);
            // 读取出原来的长度
            int length = file.ReadInt32();

            byte[] buffer = new byte[length];
            // 读取出分割的块数
            int spiltCount = file.ReadInt32();
            int remain     = file.Position;

            for (int i = 0; i < spiltCount; i++)
            {
                using (MemoryStream stream = new MemoryStream()) {
                    int blockLength = file.ReadInt32();
                    remain += 4;
                    // 定长的数据
                    stream.Write(file.Memory, remain, blockLength);
                    file.Position  += blockLength;
                    remain         += blockLength;
                    stream.Position = 0;
                    using (DeflateStream deflate = new DeflateStream(stream, CompressionMode.Decompress, true)) {
                        int readed = deflate.Read(buffer, 0, length);
                        if (readed == 0)
                        {
                            throw new FFatalException("");
                        }
                        Append(buffer, 0, readed);
                    }
                }
            }
        }
Beispiel #4
0
        //============================================================
        // <T>压缩方法</T>
        //============================================================
        public void NotBlockCompress(int size)
        {
            if (0 == size)
            {
                throw new FFatalException("Size is zero.");
            }
            FByteFile file = new FByteFile();
            // 计算分割块数
            int splitCount = _length / size;

            if (0 != (_length % size))
            {
                splitCount++;
            }
            // 写入压缩前的长度
            file.WriteUint32((UInt32)_length);
            // 写入分割块数
            file.WriteUint32((UInt32)splitCount);
            // 分段压缩数据
            int position = 0;
            int remain   = _length;

            for (int n = 0; n < splitCount; n++)
            {
                MemoryStream  stream  = new MemoryStream();
                DeflateStream deflate = new DeflateStream(stream, CompressionMode.Compress, true);
                deflate.Write(_memory, position, Math.Min(size, remain));
                position += size;
                remain   -= size;
                deflate.Close();
                // 输出压缩后数据
                int compressLength = (int)stream.Length;
                file.WriteUint32((UInt32)compressLength);
                file.WriteBytes(stream.ToArray(), 0, compressLength);
                stream.Close();
            }
            Clear();
            WriteBytes(file.ToArray(), 0, file.Length);
        }