github.com/cayleygraph/cayley@v0.7.7/internal/decompressor/decompressor.go (about) 1 // Copyright 2014 The Cayley Authors. All rights reserved. 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 package decompressor 16 17 import ( 18 "bufio" 19 "bytes" 20 "compress/bzip2" 21 "compress/gzip" 22 "io" 23 ) 24 25 const ( 26 gzipMagic = "\x1f\x8b" 27 b2zipMagic = "BZh" 28 ) 29 30 // New detects the file type of an io.Reader between 31 // bzip, gzip, or raw quad file. 32 func New(r io.Reader) (io.Reader, error) { 33 br := bufio.NewReader(r) 34 buf, err := br.Peek(3) 35 if err != nil { 36 return nil, err 37 } 38 switch { 39 case bytes.Compare(buf[:2], []byte(gzipMagic)) == 0: 40 return gzip.NewReader(br) 41 case bytes.Compare(buf[:3], []byte(b2zipMagic)) == 0: 42 return bzip2.NewReader(br), nil 43 default: 44 return br, nil 45 } 46 }