簡體   English   中英

c ++壓縮字節數組

[英]c++ compress byte array

問候所有,

我加載了一組圖像並生成了體積數據。我將這個體積數據保存在一個

unsigned char * volume

陣列。

現在我想將這個數組保存在一個文件中並檢索。但是在保存之前我想壓縮字節數組,因為卷數據很大。

關於這個的任何提示?

提前致謝。

示例中的volume不是數組。 至於壓縮,有關於該主題的書籍。 有關使用C ++快速且易於使用的內容,請查看boost.iostream庫,該庫隨zlibgzipbzip2壓縮器一起提供。

為了抵消我的挑剔,這里是一個例子(改為char因為使用unsigned char更加冗長)

#include <fstream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/copy.hpp>
namespace io = boost::iostreams;
int main()
{
    const size_t N = 1000000;
    char* volume = new char[N];
    std::fill_n(volume, N, 'a'); // 100,000 letters 'a'

    io::stream< io::array_source > source (volume, N);

    {  
      std::ofstream file("volume.bz2", std::ios::out | std::ios::binary); 
      io::filtering_streambuf<io::output> outStream; 
      outStream.push(io::bzip2_compressor()); 
      outStream.push(file); 
      io::copy(source, outStream); 
     }
    // at this point, volume.bz2 is written and closed. It is 48 bytes long
}

這取決於數據的類型。 如果圖像已經被壓縮(jpeg,png等),那么它將不再壓縮。

您可以使用zlib http://www.zlib.net/來獲取未壓縮的圖像,但我建議使用專門用於圖像的內容來壓縮每個圖像。

編輯:

1)有損壓縮將提供更高的壓縮率。

2)你提到它們的大小相同。 它們也相似嗎? 在這種情況下,視頻編解碼器將是最好的選擇。

您將需要使用第三方API(如已建議的那樣)。 如果這是C ++ / CLI,您可以使用zlib.net,但如果沒有,那么您將需要一些其他庫,如gzip或lzma。

這是7-zip sdk的鏈接: http//www.7-zip.org/sdk.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM