Guest User

meh

a guest
Feb 2nd, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. std::string Decompress(char* Data, int Size)
  2. {
  3.     std::string Result;
  4.  
  5.     z_stream zs;
  6.     std::memset(&zs, 0, sizeof(zs));
  7.  
  8.     if (inflateInit(&zs) != Z_OK)
  9.         throw(std::runtime_error("inflateInit failed while decompressing."));
  10.  
  11.     zs.next_in = (Bytef*)Data;
  12.     zs.avail_in = Size;
  13.  
  14.     int Success = 0;
  15.     std::vector<char> Temp(32768);
  16.  
  17.     do {
  18.         zs.next_out = reinterpret_cast<Bytef*>(&Temp[0]);
  19.         zs.avail_out = Temp.size();
  20.         Success = inflate(&zs, 0);
  21.  
  22.         if (Result.size() < zs.total_out)
  23.         {
  24.             Result.append(Temp.data(), zs.total_out - Result.size());
  25.         }
  26.     } while (Success == Z_OK);
  27.  
  28.     inflateEnd(&zs);
  29.  
  30.     if (Success != Z_STREAM_END)
  31.     {
  32.         std::ostringstream OS;
  33.         OS << "Exception during zlib decompression: (" << Success << ")" << zs.msg;
  34.         throw(std::runtime_error(OS.str());
  35.     }
  36.  
  37.     return Result;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment