在Golang中将一个字节切片压缩到另一个切片中

huangapple go评论90阅读模式
英文:

Zip a slice of byte into another slice in Golang

问题

我想要实现与这里给出的解决方案完全相反的功能,将一个字节切片压缩到另一个字节切片中 -

https://stackoverflow.com/questions/73717499/convert-zipped-byte-to-unzip-byte-golang-code/75321738?noredirect=1#comment132907877_75321738

类似于 -

func ZipBytes(unippedBytes []byte) ([]byte, error) {
// ...
}

[我将以多部分表单数据的形式上传该压缩文件以进行POST请求]

英文:

I want to achieve exactly opposite of the solution given here, zipping a slice of byte into another slice of byte -

https://stackoverflow.com/questions/73717499/convert-zipped-byte-to-unzip-byte-golang-code/75321738?noredirect=1#comment132907877_75321738

Something like -

func ZipBytes(unippedBytes []byte) ([]byte, error) {
// ...
}

[I am going to upload that zipped file as multipart form data for a POST request]

答案1

得分: 3

你可以直接使用bytes.Buffer将数据压缩到内存中。

以下示例使用compress/zlib,因为它与问题中给出的示例相反。根据你的用例,你也可以很容易地将其更改为compress/gzip(API非常相似)。

package data_test

import (
	"bytes"
	"compress/zlib"
	"io"
	"testing"
)

func compress(buf []byte) ([]byte, error) {
	var out bytes.Buffer
	w := zlib.NewWriter(&out)
	if _, err := w.Write(buf); err != nil {
		return nil, err
	}
	if err := w.Close(); err != nil {
		return nil, err
	}
	return out.Bytes(), nil
}

func decompress(buf []byte) (_ []byte, e error) {
	r, err := zlib.NewReader(bytes.NewReader(buf))
	if err != nil {
		return nil, err
	}
	defer func() {
		if err := r.Close(); e == nil {
			e = err
		}
	}()
	return io.ReadAll(r)
}

func TestRoundtrip(t *testing.T) {
	want := []byte("test data")

	zdata, err := compress(want)
	if err != nil {
		t.Fatalf("compress: %v", err)
	}
	got, err := decompress(zdata)
	if err != nil {
		t.Fatalf("decompress: %v", err)
	}
	if !bytes.Equal(want, got) {
		t.Errorf("roundtrip: got = %q; want = %q", got, want)
	}
}

希望对你有帮助!

英文:

You can compress directly into memory using a bytes.Buffer.

The following example uses compress/zlib since it is the opposite of the example given in the question. Depending on your use case you could easily change it to compress/gzip as well (very similar APIs).

package data_test

import (
	"bytes"
	"compress/zlib"
	"io"
	"testing"
)

func compress(buf []byte) ([]byte, error) {
	var out bytes.Buffer
	w := zlib.NewWriter(&out)
	if _, err := w.Write(buf); err != nil {
		return nil, err
	}
	if err := w.Close(); err != nil {
		return nil, err
	}
	return out.Bytes(), nil
}

func decompress(buf []byte) (_ []byte, e error) {
	r, err := zlib.NewReader(bytes.NewReader(buf))
	if err != nil {
		return nil, err
	}
	defer func() {
        if err := r.Close(); e == nil {
            e = err
        }
    }()
	return io.ReadAll(r)
}

func TestRoundtrip(t *testing.T) {
	want := []byte("test data")

	zdata, err := compress(want)
	if err != nil {
		t.Fatalf("compress: %v", err)
	}
	got, err := decompress(zdata)
	if err != nil {
		t.Fatalf("decompress: %v", err)
	}
	if !bytes.Equal(want, got) {
		t.Errorf("roundtrip: got = %q; want = %q", got, want)
	}
}

huangapple
  • 本文由 发表于 2023年2月2日 18:35:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75321865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定