| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 
 |  
bool compresserFichier(String ^cheminSource, String ^cheminDestination)
{
    try
    {
        // The file is placed in the FileStream
        FileStream ^ monFileStream = gcnew FileStream(cheminSource, FileMode::Open);
        cli::array<unsigned char,1> ^ monBuffer = gcnew cli::array<unsigned char>(safe_cast<int>(monFileStream->Length));
        // reading the FileStream
        monFileStream->Read(monBuffer, 0, safe_cast<int>(monFileStream->Length));
        // closing the FileStream
        monFileStream->Close();
        // Create the file which will contain the compressed file 
        monFileStream = gcnew FileStream(cheminDestination, FileMode::Create);
        // Data compression
        GZipStream ^monGZipStream = gcnew GZipStream(monFileStream, CompressionMode::Compress, false);
        // writing compressed data into the destination file 
        monGZipStream->Write(monBuffer, 0, monBuffer->Length);
        // Closing the GZipStream
        monGZipStream->Close();
        return true;
    }
    catch(Exception ^e)
    {
        Console::WriteLine(e->Message);
        return false;
    }
 }				
bool decompression(String ^cheminSource, String ^ cheminDestination)
{
    /*cheminSource : complete name of the compressed file
    cheminDestination : place where the file will be decompressed*/
    try
    {
        // reading the compressed file
        FileStream ^monFileStream = gcnew FileStream(cheminSource, FileMode::Open);
        // File data placed into  GZipStream
        GZipStream ^monGzipStream = gcnew GZipStream(monFileStream, CompressionMode::Decompress);
        // Array that will contain the size of the file 
        array <unsigned char> ^tailleOctets = gcnew array<unsigned char>(4);
        // get the position in the stream to get the size
        int position = safe_cast<int>(monFileStream->Length) - 4;
        monFileStream->Position = position;
        // get the file size
        monFileStream->Read(tailleOctets, 0, 4);
        // position into beginning of the stream
        monFileStream->Position = 0;
        // Converting the file size into int 
        int tailleFichier = BitConverter::ToInt32(tailleOctets, 0);
        // new size for the buffer
        array<unsigned char> ^buffer = gcnew array<unsigned char>(tailleFichier + 100);
        // Offset which permit to know its position inside the stream
        int monOffset = 0;
 
        while (true)
        {
            // Data are decompressed and put in the buffer
            int decompressionOctets = monGzipStream->Read(buffer, monOffset, 100);
            // continue until we get all the data
            if (!decompressionOctets)
                break;
            //  incrementing the offset so we won't begin  from 0 each time...
            monOffset += decompressionOctets;
         }
 
        // creating the decompressed file
        monFileStream = gcnew FileStream(cheminDestination, FileMode::Create);
        // Writing decompressed data on the file 
        monFileStream->Write(buffer, 0, tailleFichier - 1);
        // clear data from tampon memory
        monFileStream->Flush();
        // Closing streams
        monFileStream->Close();
        monGzipStream->Close();
        return true;
    }
    catch(Exception ^e)
    {
        Console::WriteLine(e->Message);
        return false;
    }
} | 
Partager