【问题标题】:Deflation compression algorithm for huge data streams大数据流的紧缩压缩算法
【发布时间】:2016-05-19 22:37:54
【问题描述】:

我的 C++ 程序不时获取数据缓冲区,应该将其添加到现有的压缩文件中。

我试图通过从某个文件中读取 1k 块来制作 POC,将它们传递给压缩流并在数据结束时解压缩它。

我使用 Poco::DeflatingOutputStream 将每个块压缩到文件中,并使用 Poco::InflatingOutputStream 来检查解压缩后是否获得了原始文件。

然而,在解压流后,我的数据似乎与原始文件几乎相同,除了在每 2 个连续的数据块之间我得到一些垃圾字符,例如:à¿_ÿ

这是一个分割成 2 个块的行的示例。原来的行是这样的:

elevated=0 path=/System/Library/CoreServices/Dock.app/Contents/MacOS/Dock exist

而解压后的行是:

elevated=0 path=/System/Libr à¿_ÿary/CoreServices/Dock.app/Contents/MacOS/Dock exist

5 月 19 日 19:12:51 PANMMUZNG8WNREM 内核[0]:pid=904 uid=1873876126 sbit=0

知道我做错了什么。这是我的 POC 代码:

int zip_unzip() {  
   std::ostringstream stream1;
   Poco::DeflatingOutputStream gzipper(stream1, Poco::DeflatingStreamBuf::STREAM_ZLIB);

   std::ifstream bigFile("/tmp/in.log");
   constexpr size_t bufferSize = 1024;
   char buffer[bufferSize];
   while (bigFile) {
       bigFile.read(buffer, bufferSize);
       gzipper << buffer;
   }
   gzipper.close();

   std::string zipped_string = stream1.str();
   ////////////////// 
   std::ofstream stream2("/tmp/out.log", std::ios::binary);
   Poco::InflatingOutputStream gunzipper(stream2, InflatingStreamBuf::STREAM_ZLIB);
   gunzipper << zipped_string;
   gunzipper.close();
   return 0;
}

【问题讨论】:

    标签: c++ compression poco-libraries deflate inflate


    【解决方案1】:

    好的,我刚刚意识到我在每次从 HugeFile(原始解压缩文件)读取时都使用了“

    这是固定版本:

    #include <stdio.h>
    #include <fstream>
    #include <Poco/DeflatingStream.h>
    #include <Poco/Exception.h>
    #include <iostream>
    
    
    int BetterZip()
    {
        try {
        // Create gzip file.
        std::ofstream output_file("/tmp/out.gz", std::ios::binary);
        Poco::DeflatingOutputStream output_stream(output_file, Poco::DeflatingStreamBuf::STREAM_GZIP);
    
        // INPUT
        std::ifstream big_file("/tmp/hugeFile");
        constexpr size_t ReadBufferSize = 1024;
        char buffer[ReadBufferSize];
        while (big_file) {
            big_file.read(buffer, ReadBufferSize);
            output_stream.write(buffer, big_file.gcount());
        }
    
        output_stream.close();
        } catch (const Poco::Exception& ex) {
            std::cout << "Error :  (error code " << ex.code() << " ("  << ex.displayText() << ")";
            return EINVAL;
        }
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多