Lzma (7-zip) and zlib

Source: Internet
Author: User
Tags uncompress

Lzma (7-zip:

In the C directoryAlgorithmFile, go to the util \ lzmalib directory, compile and generate the Lib library, and export the following two functions. lzmacompress is the compression function, and lzmauncompress is the decompression function.

My_stdapi lzmacompress (unsigned char * DEST, size_t * destlen, const unsigned char * SRC, size_t srclen,
Unsigned char * outprops, size_t * outpropssize,
Int level,
Unsigned dictsize,
Int LC,
Int LP,
Int Pb,
Int FB,
Int numthreads
);

My_stdapi lzmauncompress (unsigned char * DEST, size_t * destlen, const unsigned char * SRC, sizet * srclen,
Const unsigned char * props, size_t propssize );

Import types. h and lzmalib. H to the project.

CodeAs follows:

# Include "stdafx. H"
# Include "lzmalib. H"

# Pragma comment (Lib, "lzma. lib ")

Int _ tmain (INT argc, _ tchar * argv [])
{
File * pfile = _ tfopen (_ T ("file. dat"), _ T ("rb "));

If (pfile = NULL)
{
_ Ftprintf (stderr, _ T ("error to open the file! "));
Return-1;
}

Fseek (pfile, 0, seek_end );
Size_t srclen = ftell (pfile );
Rewind (pfile );
Size_t destlen = srclen * 2;
Unsigned char * psrcread = new unsigned char [srclen]; // raw file data
Unsigned char * pdecomress = new unsigned char [srclen]; // stores the extracted data
Unsigned char * plzma = new unsigned char [destlen]; // stores compressed data

Fread (psrcread, sizeof (char), srclen, pfile );

Unsigned char prop [5] =
{
0
};
Size_t sizeprop = 5;

If (sz_ OK! = Lzmacompress (plzma, & destlen, psrcread, srclen, prop,
& sizeprop, 9, (1 <24), 3, 0, 2, 32, 2 ))
{< br> // error
_ ftprintf (stderr, _ T ("error during compression! ");
Delete [] psrcread;
Delete [] pdecomress;
Delete [] plzma;
fclose (pfile );
return-1;
}

file * pcompressfile = _ tfopen (_ T ("compress. dat "), _ T (" WB ");
// write compressed data
If (pcompressfile = NULL)
{< br> _ ftprintf (stderr, _ T ("An error occurred while creating the file! ");
Delete [] psrcread;
Delete [] pdecomress;
Delete [] plzma;
fclose (pfile );
return-1;
}< br> fwrite (plzma, sizeof (char), destlen, pcompressfile);
fclose (pcompressfile);

file * pdecompressfile = _ tfopen (_ T ("decompress. dat "), _ T (" WB ");
// write and extract data
If (pdecompressfile = NULL)
{< br> _ ftprintf (stderr, _ T ("An error occurred while writing data! ");
Delete [] psrcread;
Delete [] pdecomress;
Delete [] plzma;
fclose (pfile );
return-1;
}

// Note: during decompression, the props parameter must use the outprops generated during compression to decompress normally.
If (sz_ OK! = Lzmauncompress (pdecomress, & srclen, plzma, & destlen, prop, 5 ))
{
Delete [] psrcread;
Delete [] pdecomress;
Delete [] plzma;

Fclose (pdecompressfile );
Fclose (pfile );
Return-1;
}

Fwrite (pdecomress, sizeof (char), srclen, pdecompressfile );

Delete [] psrcread;
Delete [] pdecomress;
Delete [] plzma;

Fclose (pdecompressfile );
Fclose (pfile );

Return 0;
}

Zlib usage:

Zlib is a common compression library that provides a set of In-memory compression and decompression functions, and can detect the integrity of extracted data (integrity ). The following describes the two most useful functions: compress and uncompress.

Int compress (bytef * DEST, ulogfn * destlen, const bytef * Source, ulong sourcelen );

The compress function compresses the content in the source buffer to the Dest buffer. Sourcelen indicates the size of the source buffer (in bytes ). Note that the second parameter destlen of the function is the address transfer call. When a function is called, destlen indicates the size of the Dest buffer. destlen> (sourcelen + 12) * 100.1%. After the function exits, destlen indicates the actual size of the buffer after compression. In this case, destlen/sourcelen is the compression ratio.

If compress succeeds, z_ OK is returned. If there is not enough memory, z_mem_error is returned. If the output buffer is not large enough, z_buf_error is returned.

Int uncompress (bytef * DEST, ulogfn * destlen, const bytef * Source, ulong sourcelen );

The uncompress function extracts the content of the source buffer to the Dest buffer. Sourcelen is the size of the source buffer (in bytes ). Note that the second parameter destlen of the function is the address transfer call. When a function is called, destlen indicates the size of the Dest buffer, and the Dest buffer must be sufficient to accommodate the extracted data. During decompression, you need to know in advance how large the compressed data will be. This requires that the size of the original data (that is, the size of the extracted data) be saved before compression ). This is not a function of the zlib function library and requires additional work. After the function exits, destlen indicates the actual size of the extracted data.

If uncompress succeeds, z_ OK is returned. If there is not enough memory, z_mem_error is returned. If the output buffer is not large enough, z_buf_error is returned. If the input data is incorrect, z_data_error is returned.

The Code is as follows:

# Include "stdafx. H"
# Include <cstring>
# Include <cstdlib>
# Include <iostream>
# Include "zlib. H"
Using namespace STD;

Int _ tmain (INT argc, _ tchar * argv [])
{
Int err = 0;
Byte compr [200] = {0}, uncompr [200] = {0}; // big enough
Ulong comprlen = 0, uncomprlen = 0;

Const char * Hello = "12345678901234567890123456789012345678901234567890 ";

Ulong Len = strlen (Hello) + 1;
Comprlen = sizeof (compr)/sizeof (compr [0]);

Err = compress (compr, & comprlen, (const bytef *) Hello, Len );

If (Err! = Z_ OK)
{
Cerr <"compess error:" <err <'\ n ';
Exit (1 );
}

Cout <"orignal size:" <Len
<", Compressed size:" <comprlen <'\ n ';

Strcpy (char *) uncompr, "garbage ");

Err = uncompress (uncompr, & uncomprlen, compr, comprlen );

If (Err! = Z_ OK)
{
Cerr <"uncompess error:" <err <'\ n ';
Exit (1 );
}

Cout <"orignal size:" <Len
<", Uncompressed size:" <uncomprlen <'\ n ';

If (strcmp (char *) uncompr, hello ))
{
Cerr <"Bad uncompress !!! \ N ";
Exit (1 );
}
Else
{
Cout <"uncompress () succeed: \ n" <(char *) uncompr;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.