title: Can zlib remove padding without compressing the rest of the file?
body: <p>I'm looking for a way to apply a specific compression strategy using zlib-1.2.5.
  I need to force zlib to pack a file without actually compressing it. The only part I want to compress is 0 padding at the end of the file, so that the output file does not have it. The size of input files ranges from 1MB to 1GB, padding is 512B.
  Is this achievable with zlib?</p>
  <p>Edit:</p>
  <p>The code I'm working on is based on Unreal Engine 4:
  <a href="https://github.com/EpicGames/UnrealEngine/blob/release/Engine/Source/Runtime/Core/Private/Misc/Compression.cpp" rel="nofollow noreferrer">https://github.com/EpicGames/UnrealEngine/blob/release/Engine/Source/Runtime/Core/Private/Misc/Compression.cpp</a></p>
  <pre><code>static bool appCompressMemoryZLIB(void* CompressedBuffer, int32&amp; CompressedSize, const void* UncompressedBuffer, int32 UncompressedSize, int32 BitWindow, int32 CompLevel)
  {
      DECLARE_SCOPE_CYCLE_COUNTER(TEXT(&quot;Compress Memory ZLIB&quot;), STAT_appCompressMemoryZLIB, STATGROUP_Compression);
  
      ensureMsgf(CompLevel &gt;= Z_DEFAULT_COMPRESSION, TEXT(&quot;CompLevel must be &gt;= Z_DEFAULT_COMPRESSION&quot;));
      ensureMsgf(CompLevel &lt;= Z_BEST_COMPRESSION, TEXT(&quot;CompLevel must be &lt;= Z_BEST_COMPRESSION&quot;));
  
      CompLevel = FMath::Clamp(CompLevel, Z_DEFAULT_COMPRESSION, Z_BEST_COMPRESSION);
  
      // Zlib wants to use unsigned long.
      unsigned long ZCompressedSize = CompressedSize;
      unsigned long ZUncompressedSize = UncompressedSize;
      bool bOperationSucceeded = false;
  
      // Compress data
      // If using the default Zlib bit window, use the zlib routines, otherwise go manual with deflate2
      if (BitWindow == 0 || BitWindow == DEFAULT_ZLIB_BIT_WINDOW)
      {
          bOperationSucceeded = compress2((uint8*)CompressedBuffer, &amp;ZCompressedSize, (const uint8*)UncompressedBuffer, ZUncompressedSize, CompLevel) == Z_OK ? true : false;
      }
      else
      {
          z_stream stream;
          stream.next_in = (Bytef*)UncompressedBuffer;
          stream.avail_in = (uInt)ZUncompressedSize;
          stream.next_out = (Bytef*)CompressedBuffer;
          stream.avail_out = (uInt)ZCompressedSize;
          stream.zalloc = &amp;zalloc;
          stream.zfree = &amp;zfree;
          stream.opaque = Z_NULL;
  
          if (ensure(Z_OK == deflateInit2(&amp;stream, CompLevel, Z_DEFLATED, BitWindow, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY)))
          {
              if (ensure(Z_STREAM_END == deflate(&amp;stream, Z_FINISH)))
              {
                  ZCompressedSize = stream.total_out;
                  if (ensure(Z_OK == deflateEnd(&amp;stream)))
                  {
                      bOperationSucceeded = true;
                  }
              }
              else
              {
                  deflateEnd(&amp;stream);
              }
          }
      }
  
      // Propagate compressed size from intermediate variable back into out variable.
      CompressedSize = ZCompressedSize;
      return bOperationSucceeded;
  }
  </code></pre>
  <p>With params: input buffer size = 65kB, CompLevel=Z_DEFAULT_COMPRESSION, MAX_MEM_LEVEL=9, BitWindow=15</p>