Copyright 2011 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

package packet

import (
	
	
	
	
	
	
)
Compressed represents a compressed OpenPGP packet. The decompressed contents will contain more OpenPGP packets. See RFC 4880, section 5.6.
CompressionConfig contains compressor configuration settings.
Level is the compression level to use. It must be set to between -1 and 9, with -1 causing the compressor to use the default compression level, 0 causing the compressor to use no compression and 1 to 9 representing increasing (better, slower) compression levels. If Level is less than -1 or more then 9, a non-nil error will be returned during encryption. See the constants above for convenient common settings for Level.
	Level int
}

func ( *Compressed) ( io.Reader) error {
	var  [1]byte
	,  := readFull(, [:])
	if  != nil {
		return 
	}

	switch [0] {
	case 1:
		.Body = flate.NewReader()
	case 2:
		.Body,  = zlib.NewReader()
	case 3:
		.Body = bzip2.NewReader()
	default:
		 = errors.UnsupportedError("unknown compression algorithm: " + strconv.Itoa(int([0])))
	}

	return 
}
compressedWriterCloser represents the serialized compression stream header and the compressor. Its Close() method ensures that both the compressor and serialized stream header are closed. Its Write() method writes to the compressor.
type compressedWriteCloser struct {
	sh io.Closer      // Stream Header
	c  io.WriteCloser // Compressor
}

func ( compressedWriteCloser) ( []byte) (int, error) {
	return .c.Write()
}

func ( compressedWriteCloser) () ( error) {
	 = .c.Close()
	if  != nil {
		return 
	}

	return .sh.Close()
}
SerializeCompressed serializes a compressed data packet to w and returns a WriteCloser to which the literal data packets themselves can be written and which MUST be closed on completion. If cc is nil, sensible defaults will be used to configure the compression algorithm.
func ( io.WriteCloser,  CompressionAlgo,  *CompressionConfig) ( io.WriteCloser,  error) {
	,  := serializeStreamHeader(, packetTypeCompressed)
	if  != nil {
		return
	}

	_,  = .Write([]byte{uint8()})
	if  != nil {
		return
	}

	 := DefaultCompression
	if  != nil {
		 = .Level
	}

	var  io.WriteCloser
	switch  {
	case CompressionZIP:
		,  = flate.NewWriter(, )
	case CompressionZLIB:
		,  = zlib.NewWriterLevel(, )
	default:
		 := strconv.Itoa(int())
		 = errors.UnsupportedError("Unsupported compression algorithm: " + )
	}
	if  != nil {
		return
	}

	 = compressedWriteCloser{, }

	return