gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/tools/embeddedbinary/flatecompress.go (about)

     1  // Copyright 2023 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // flatecompress compresses data from stdin and writes it to stdout with flate.
    16  package main
    17  
    18  import (
    19  	"compress/flate"
    20  	"fmt"
    21  	"io"
    22  	"os"
    23  )
    24  
    25  func main() {
    26  	writer, err := flate.NewWriter(os.Stdout, flate.BestCompression)
    27  	if err != nil {
    28  		fmt.Fprintf(os.Stderr, "Cannot create flate writer: %v\n", err)
    29  		os.Exit(1)
    30  	}
    31  	if _, err := io.Copy(writer, os.Stdin); err != nil {
    32  		fmt.Fprintf(os.Stderr, "Failed to compress binary: %v\n", err)
    33  		os.Exit(1)
    34  	}
    35  	if err := writer.Flush(); err != nil {
    36  		fmt.Fprintf(os.Stderr, "Cannot flush: %v\n", err)
    37  		os.Exit(1)
    38  	}
    39  	if err := writer.Close(); err != nil {
    40  		fmt.Fprintf(os.Stderr, "Cannot close writer: %v\n", err)
    41  		os.Exit(1)
    42  	}
    43  	os.Exit(0)
    44  }