private static byte[] InternalCompress(byte[] bytes, bool useHeader, CompressionImplementation compressionImplementation) { switch (compressionImplementation) { case CompressionImplementation.ManagedZLib: return(ManagedZLibWrapper.Compress(bytes, zLibCompressionAmount, useHeader)); default: throw new ApplicationException(string.Format("Unknown compression implementation {0}", compressionImplementation)); } }
private static byte[] InternalDecompress(byte[] bytes, bool useHeader, CompressionImplementation compressionImplementation) { switch (compressionImplementation) { case CompressionImplementation.ManagedZLib: byte[] decompressed = null; try { decompressed = ManagedZLibWrapper.Decompress(bytes, useHeader); } catch (Exception e) { if (useHeader) { if (e.Message.Contains("Invalid GZip header")) { //we're probably trying to decompress data that has no header with the header flag. if it throws again, just let it go log.WarnFormat("Tried to decompress using header and got error {0}. Attempting decompress with no header.", e.Message); try { decompressed = ManagedZLibWrapper.Decompress(bytes, false); } catch (Exception e2) { if (e2.Message.Contains("Z_DATA_ERROR")) //then the data is actually invalid; it may not be compressed at all. we'll try just returning the data as it was { log.WarnFormat("Tried to decompress with no header after getting error with header and got error {0}. Returning bytes as they were.", e2.Message); decompressed = bytes; } else { throw; } } } else { throw; } } else { if (e.Message.Contains("Z_DATA_ERROR")) { //probably the inverse; tried to decompress data with a header without one. log.WarnFormat("Tried to decompress with no header and got error {0}. Attempting decompress with header.", e.Message); try { decompressed = ManagedZLibWrapper.Decompress(bytes, true); } catch (Exception e2) { if (e2.Message.Contains("Invalid GZip header")) { log.WarnFormat("Tried to decompress with header after getting error with header and got error {0}. Returning byte as they were.", e2.Message); decompressed = bytes; } else { throw; } } } else { throw; } } } return(decompressed); default: throw new ApplicationException(string.Format("Unknown compression implementation {0}", compressionImplementation)); } }