c++ zlib(qt)压缩与解压缩

简介: #include #include "zlib.h"#include "stdio.h"#include using   namespace   std; #define MaxBufferSize 999999 void Test1();void Test2(); ...

#include <QtCore/QCoreApplication>

#include "zlib.h"
#include "stdio.h"
#include <iostream>
using    namespace    std;
 
#define MaxBufferSize 999999
 
void  Test1();
void  Test2();
 
int  main( int  argc, char  *argv[])
{
     QCoreApplication a(argc, argv);
 
     //Test1();
     Test2();
     
     return  a.exec();
}
 
//c++ zlib
void  Test1()
{
     FILE* File_src;
     FILE* File_compress;
     FILE* File_uncompress;
 
     unsigned long  len_src;
     unsigned long  len_compress;
     unsigned long  len_uncompress;
 
     unsigned char  *buffer_src  = new  unsigned char [MaxBufferSize];
     unsigned char  *buffer_compress  = new  unsigned char [MaxBufferSize];
     unsigned char  *buffer_uncompress = new  unsigned char [MaxBufferSize];
 
     File_src = fopen( "src.txt" , "r" );
     File_compress = fopen( "compress.txt" , "w" );
     File_uncompress = fopen( "uncompress.txt" , "w" );
 
     //compress
     len_src = fread(buffer_src, sizeof ( char ),MaxBufferSize-1,File_src);
     compress(buffer_compress,&len_compress,buffer_src,len_src);
     fwrite(buffer_compress, sizeof ( char ),len_compress,File_compress);
     cout << "normal zlib:"  << endl;
     cout << "src:\n"  << buffer_src << ",length:"  << len_src << endl << endl;
     cout << "compress:\n"  << buffer_compress << ",length:"  << len_compress << endl << endl;
 
     //uncompress
     uncompress(buffer_uncompress,&len_uncompress,buffer_compress,len_compress);
     fwrite(buffer_uncompress, sizeof ( char ),len_uncompress,File_uncompress);
     cout << "uncompress:\n"  << buffer_uncompress << ",length:"  << len_uncompress << endl << endl;
 
     fclose(File_src);
     fclose(File_compress);
     fclose(File_uncompress);
}
 
//qt zlib
void  Test2()
{
     QByteArray src;
     src.append( "中华人民共和国,china mobile,123456" );
 
     unsigned long  len_compress;
     unsigned long  len_uncompress;
 
     unsigned char  *buffer_compress  = new  unsigned char [MaxBufferSize];
     unsigned char  *buffer_uncompress = new  unsigned char [MaxBufferSize];
 
     compress(buffer_compress,&len_compress,(Bytef*)src.data(), src.length());
     uncompress(buffer_uncompress,&len_uncompress,buffer_compress,len_compress);
 
     cout << "qt zlib:"  << endl;
     cout << "src:\n"  << src.data() << ",length:"  << src.size() << endl << endl;
     cout << "compress:\n"  << buffer_compress << ",length:"  << len_compress << endl << endl;
     cout << "uncompress:\n"  << buffer_uncompress << ",length:"  << len_uncompress << endl << endl;
}
相关文章
|
4月前
|
存储 算法 安全
C/C++ Zlib实现文件压缩与解压
在软件开发和数据处理中,对数据进行高效的压缩和解压缩是一项重要的任务。这不仅有助于减小数据在网络传输和存储中的占用空间,还能提高系统的性能和响应速度。本文将介绍如何使用 zlib 库进行数据的压缩和解压缩,以及如何保存和读取压缩后的文件。zlib 是一个开源的数据压缩库,旨在提供高效、轻量级的压缩和解压缩算法。其核心压缩算法基于 DEFLATE,这是一种无损数据压缩算法,通常能够提供相当高的压缩比。zlib 库广泛应用于多个领域,包括网络通信、文件压缩、数据库系统等。
200 0
C/C++ Zlib实现文件压缩与解压
|
10月前
|
算法 Python
Python中的数据压缩与解压缩:深入了解zlib模块
Python有一些内置库用于处理数据压缩和解压缩,其中一个就是`zlib`模块。这个模块为DEFLATE压缩算法和相关的`gzip`(文件格式)提供了支持。在这篇文章中,我们将深入探讨如何使用`zlib`模块进行数据压缩和解压缩。
|
C++
c++ zlib(qt)压缩与解压缩
#include #include "zlib.h" #include "stdio.h" #include using   namespace   std;   #define MaxBufferSize 999999   void Test1(); void Test2();  ...
2081 0
|
关系型数据库 应用服务中间件 Linux

热门文章

最新文章

推荐镜像

更多