2018-02-28 06:19:06 +00:00
|
|
|
package archive
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TarFileInBuffer will create a tar archive containing a single file named via fileName and using the content
|
|
|
|
// specified in fileContent. Returns the archive as a byte array.
|
2018-10-22 21:03:30 +00:00
|
|
|
func TarFileInBuffer(fileContent []byte, fileName string, mode int64) ([]byte, error) {
|
2018-02-28 06:19:06 +00:00
|
|
|
var buffer bytes.Buffer
|
|
|
|
tarWriter := tar.NewWriter(&buffer)
|
|
|
|
|
|
|
|
header := &tar.Header{
|
|
|
|
Name: fileName,
|
2018-10-22 21:03:30 +00:00
|
|
|
Mode: mode,
|
2018-02-28 06:19:06 +00:00
|
|
|
Size: int64(len(fileContent)),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := tarWriter.WriteHeader(header)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tarWriter.Write(fileContent)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tarWriter.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer.Bytes(), nil
|
|
|
|
}
|