You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

bzip2_compress.go 1.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package compressor
  18. import (
  19. "bytes"
  20. "io/ioutil"
  21. "github.com/dsnet/compress/bzip2"
  22. )
  23. type Bzip2 struct {
  24. }
  25. // Compress Bzip2 compress
  26. func (g *Bzip2) Compress(b []byte) ([]byte, error) {
  27. var buffer bytes.Buffer
  28. gz, err := bzip2.NewWriter(&buffer, &bzip2.WriterConfig{Level: bzip2.DefaultCompression})
  29. if err != nil {
  30. return nil, err
  31. }
  32. if _, err := gz.Write(b); err != nil {
  33. return nil, err
  34. }
  35. if err := gz.Close(); err != nil {
  36. return nil, err
  37. }
  38. return buffer.Bytes(), nil
  39. }
  40. // Decompress Bzip2 decompress
  41. func (g *Bzip2) Decompress(in []byte) ([]byte, error) {
  42. reader, err := bzip2.NewReader(bytes.NewReader(in), nil)
  43. if err != nil {
  44. return nil, err
  45. }
  46. if err = reader.Close(); err != nil {
  47. return nil, err
  48. }
  49. output, err := ioutil.ReadAll(reader)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return output, nil
  54. }
  55. func (g *Bzip2) GetCompressorType() CompressorType {
  56. return CompressorBzip2
  57. }