Ejemplo n.º 1
0
    bool UncompressFile(string iRoot, BinaryReader reader)
    {
        Zlib.FileHeader fileHeader = default(Zlib.FileHeader);
        string          str        = reader.ReadString();

        fileHeader.FileName         = FileIO.StripSlash(iRoot) + str;
        fileHeader.DecompressedSize = reader.ReadInt32();
        fileHeader.CompressedSize   = reader.ReadInt32();
        this._sFrm.StatusText2      = str;
        if (!Directory.Exists(fileHeader.FileName.Substring(0, fileHeader.FileName.LastIndexOf("\\", StringComparison.Ordinal))))
        {
            Directory.CreateDirectory(fileHeader.FileName.Substring(0, fileHeader.FileName.LastIndexOf("\\", StringComparison.Ordinal)));
        }
        if (File.Exists(fileHeader.FileName))
        {
            File.Delete(fileHeader.FileName);
        }
        FileStream   fileStream;
        BinaryWriter binaryWriter;
        bool         flag;

        try
        {
            fileStream   = new FileStream(fileHeader.FileName, FileMode.Create);
            binaryWriter = new BinaryWriter(fileStream);
        }
        catch (Exception ex)
        {
            MessageBox.Show("An error occurred while opening files for writing for decompression: " + ex.Message, "Buh?");
            flag = false;
            return(flag);
        }
        try
        {
            byte[] iBytes = reader.ReadBytes(fileHeader.CompressedSize);
            byte[] buffer = Zlib.UncompressChunk(ref iBytes, fileHeader.DecompressedSize);
            if (buffer.Length != fileHeader.DecompressedSize)
            {
                throw new Exception("Uncompressed data was not the expected size!");
            }
            binaryWriter.Write(buffer);
            binaryWriter.Close();
            fileStream.Close();
            flag = true;
        }
        catch (Exception ex2)
        {
            MessageBox.Show("An error occurred during decompression: " + ex2.Message, "Buh?");
            reader.Close();
            fileStream.Close();
            flag = false;
        }
        return(flag);
    }
Ejemplo n.º 2
0
    static bool CompressFile(string iFileName, string iDest, BinaryWriter writer)
    {
        int int32 = Convert.ToInt32(new FileInfo(iFileName).Length);

        Zlib.FileHeader fileHeader = default(Zlib.FileHeader);
        fileHeader.FileName         = iDest;
        fileHeader.DecompressedSize = int32;
        fileHeader.CompressedSize   = 0;
        FileStream   fileStream;
        BinaryReader binaryReader;
        bool         flag;

        try
        {
            fileStream   = new FileStream(iFileName, FileMode.Open);
            binaryReader = new BinaryReader(fileStream);
        }
        catch (Exception ex)
        {
            MessageBox.Show("An error occurred while compressing: " + ex.Message, "Buh?");
            flag = false;
            return(flag);
        }
        try
        {
            byte[] iBytes = binaryReader.ReadBytes(int32);
            byte[] buffer = Zlib.CompressChunk(ref iBytes);
            fileHeader.CompressedSize = buffer.Length;
            if (fileHeader.CompressedSize < 1)
            {
                throw new Exception("Compressed data was 0 bytes long!");
            }
            writer.Write(fileHeader.FileName);
            writer.Write(fileHeader.DecompressedSize);
            writer.Write(fileHeader.CompressedSize);
            writer.Write(buffer);
            binaryReader.Close();
            fileStream.Close();
            flag = true;
        }
        catch (Exception ex2)
        {
            MessageBox.Show("An error occurred during compression: " + ex2.Message, "Buh?");
            binaryReader.Close();
            fileStream.Close();
            flag = false;
        }
        return(flag);
    }