本文整理匯總了Golang中compress/bzip2.NewReader函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewReader函數的具體用法?Golang NewReader怎麽用?Golang NewReader使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewReader函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: extractBz2
func extractBz2(body []byte, location string) (string, error) {
bodyCopy := make([]byte, len(body))
copy(bodyCopy, body)
tarFile := bzip2.NewReader(bytes.NewReader(body))
tarReader := tar.NewReader(tarFile)
var dirList []string
for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
dirList = append(dirList, header.Name)
}
basedir := findBaseDir(dirList)
tarFile = bzip2.NewReader(bytes.NewReader(bodyCopy))
tarReader = tar.NewReader(tarFile)
for {
header, err := tarReader.Next()
if err == io.EOF {
break
} else if err != nil {
//return location, err
}
path := filepath.Join(location, strings.Replace(header.Name, basedir, "", -1))
info := header.FileInfo()
if info.IsDir() {
if err = os.MkdirAll(path, info.Mode()); err != nil {
return location, err
}
continue
}
if header.Typeflag == tar.TypeSymlink {
err = os.Symlink(header.Linkname, path)
continue
}
file, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, info.Mode())
if err == nil {
defer file.Close()
}
_, err = io.Copy(file, tarReader)
if err != nil {
//return location, err
}
}
return location, nil
}
示例2: tarFilesCount
func tarFilesCount(sourcefile string) int {
flreader, _ := os.Open(sourcefile)
defer flreader.Close()
var fltarReader *tar.Reader
var flReader io.ReadCloser = flreader
if strings.HasSuffix(sourcefile, ".gz") ||
strings.HasSuffix(sourcefile, ".tgz") {
flgzipReader, err := gzip.NewReader(flreader)
checkErr("In tarFilesCounter - NewReader", err)
fltarReader = tar.NewReader(flgzipReader)
defer flReader.Close()
} else if strings.HasSuffix(sourcefile, ".bz2") {
flbz2Reader := bzip2.NewReader(flreader)
fltarReader = tar.NewReader(flbz2Reader)
} else {
fltarReader = tar.NewReader(flreader)
}
trfl := fltarReader
counter := 0
for {
_, err := trfl.Next()
if err != nil {
if err == io.EOF {
break
}
checkErr("Extract error::ReadTarArchive", err)
}
counter++
}
fmt.Println("Files in archive -", counter)
return counter
}
示例3: TestBzip2
func TestBzip2(t *testing.T) {
var compressed, uncompressed bytes.Buffer
w := bzip.NewWriter(&compressed)
// Write a repetitive message in a million pieces,
// compressing one copy but not the other.
tee := io.MultiWriter(w, &uncompressed)
for i := 0; i < 1000000; i++ {
io.WriteString(tee, "hello")
}
if err := w.Close(); err != nil {
t.Fatal(err)
}
// Check the size of the compressed stream.
if got, want := compressed.Len(), 255; got != want {
t.Errorf("1 million hellos compressed to %d bytes, want %d", got, want)
}
// Decompress and compare with original.
var decompressed bytes.Buffer
io.Copy(&decompressed, bzip2.NewReader(&compressed))
if !bytes.Equal(uncompressed.Bytes(), decompressed.Bytes()) {
t.Error("decompression yielded a different message")
}
}
示例4: skipToArMember
func skipToArMember(arReader *ar.Reader, memberPrefix string) (io.Reader, error) {
var err error
// find the right ar member
var header *ar.Header
for {
header, err = arReader.Next()
if err != nil {
return nil, err
}
if strings.HasPrefix(header.Name, memberPrefix) {
break
}
}
// figure out what compression to use
var dataReader io.Reader
switch {
case strings.HasSuffix(header.Name, ".gz"):
dataReader, err = gzip.NewReader(arReader)
if err != nil {
return nil, err
}
case strings.HasSuffix(header.Name, ".bz2"):
dataReader = bzip2.NewReader(arReader)
case strings.HasSuffix(header.Name, ".xz"):
dataReader = xzPipeReader(arReader)
default:
return nil, fmt.Errorf("Can not handle %s", header.Name)
}
return dataReader, nil
}
示例5: OpenDecompress
func OpenDecompress(fn string) (r io.ReadCloser, err error) {
f, err := os.Open(fn)
if err != nil {
return nil, err
}
ext := filepath.Ext(fn)
switch ext {
case ".log", ".txt":
return f, err
case ".gz":
gzr, err := gzip.NewReader(f)
return &WrappedDecompressor{
ReadCloser: gzr,
wrapped: f,
}, err
case ".bz2":
bzr := bzip2.NewReader(f)
return &WrappedDecompressor{
ReadCloser: ioutil.NopCloser(bzr),
wrapped: f,
}, nil
default:
return f, err
}
}
示例6: maybeDecompress
func maybeDecompress(rs io.ReadSeeker) (io.Reader, error) {
// TODO(jonboulle): this is a bit redundant with detectValType
typ, err := aci.DetectFileType(rs)
if err != nil {
return nil, err
}
if _, err := rs.Seek(0, 0); err != nil {
return nil, err
}
var r io.Reader
switch typ {
case aci.TypeGzip:
r, err = gzip.NewReader(rs)
if err != nil {
return nil, fmt.Errorf("error reading gzip: %v", err)
}
case aci.TypeBzip2:
r = bzip2.NewReader(rs)
case aci.TypeXz:
r = aci.XzReader(rs)
case aci.TypeTar:
r = rs
case aci.TypeUnknown:
return nil, errors.New("unknown filetype")
default:
// should never happen
panic(fmt.Sprintf("bad type returned from DetectFileType: %v", typ))
}
return r, nil
}
示例7: getSampleLines
func getSampleLines(path string) []string {
file, err := os.Open(path)
if err != nil {
panic("Could not open sample file: " + err.Error())
}
defer file.Close()
bz := bzip2.NewReader(file)
rv := make([]string, 0, 250000)
bio := bufio.NewReader(bz)
bytesread := int64(0)
done := false
for !done {
line, err := bio.ReadString('\n')
switch err {
case nil:
rv = append(rv, line)
bytesread += int64(len(line))
case io.EOF:
done = true
default:
panic("Could not load samples: " + err.Error())
}
}
return rv
}
示例8: DecompressStream
func DecompressStream(archive io.Reader) (io.ReadCloser, error) {
buf := make([]byte, 10)
totalN := 0
for totalN < 10 {
n, err := archive.Read(buf[totalN:])
if err != nil {
if err == io.EOF {
return nil, fmt.Errorf("Tarball too short")
}
return nil, err
}
totalN += n
utils.Debugf("[tar autodetect] n: %d", n)
}
compression := DetectCompression(buf)
wrap := io.MultiReader(bytes.NewReader(buf), archive)
switch compression {
case Uncompressed:
return ioutil.NopCloser(wrap), nil
case Gzip:
return gzip.NewReader(wrap)
case Bzip2:
return ioutil.NopCloser(bzip2.NewReader(wrap)), nil
case Xz:
return xzDecompress(wrap)
default:
return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
}
}
示例9: ls
func ls(path string) (map[string]bool, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
files := make(map[string]bool)
bz2R := bzip2.NewReader(f)
r := tar.NewReader(bz2R)
for {
h, err := r.Next()
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
files[h.Name] = true
}
// don't exclude the metadata
delete(files, "./")
delete(files, "repositories")
return files, nil
}
示例10: loadBlocks
func loadBlocks(t *testing.T, file string) (blocks []*dcrutil.Block, err error) {
fi, err := os.Open(file)
if err != nil {
t.Errorf("failed to open file %v, err %v", file, err)
return nil, err
}
bcStream := bzip2.NewReader(fi)
defer fi.Close()
// Create a buffer of the read file
bcBuf := new(bytes.Buffer)
bcBuf.ReadFrom(bcStream)
// Create decoder from the buffer and a map to store the data
bcDecoder := gob.NewDecoder(bcBuf)
blockchain := make(map[int64][]byte)
// Decode the blockchain into the map
if err := bcDecoder.Decode(&blockchain); err != nil {
t.Errorf("error decoding test blockchain")
}
blocks = make([]*dcrutil.Block, 0, len(blockchain))
for height := int64(1); height < int64(len(blockchain)); height++ {
block, err := dcrutil.NewBlockFromBytes(blockchain[height])
if err != nil {
t.Errorf("failed to parse block %v", height)
return nil, err
}
block.SetHeight(height - 1)
blocks = append(blocks, block)
}
return
}
示例11: unpackRootfs
// unpackRootfs unpacks a stage1 rootfs (compressed file, pointed to by rfs)
// into dir, returning any error encountered
func unpackRootfs(rfs string, dir string) error {
fh, err := os.Open(rfs)
if err != nil {
return fmt.Errorf("error opening stage1 rootfs: %v", err)
}
typ, err := aci.DetectFileType(fh)
if err != nil {
return fmt.Errorf("error detecting image type: %v", err)
}
if _, err := fh.Seek(0, 0); err != nil {
return fmt.Errorf("error seeking image: %v", err)
}
var r io.Reader
switch typ {
case aci.TypeGzip:
r, err = gzip.NewReader(fh)
if err != nil {
return fmt.Errorf("error reading gzip: %v", err)
}
case aci.TypeBzip2:
r = bzip2.NewReader(fh)
case aci.TypeXz:
r = aci.XzReader(fh)
case aci.TypeTar:
r = fh
case aci.TypeUnknown:
return fmt.Errorf("error: unknown image filetype")
default:
// should never happen
panic("no type returned from DetectFileType?")
}
return untarRootfs(r, dir)
}
示例12: Open
func (b *benchmarkCase) Open() (io.ReadCloser, error) {
buf := bytes.NewBufferString(b.casefile)
return &benchmarkFile{
Reader: bzip2.NewReader(base64.NewDecoder(base64.StdEncoding, buf)),
buffer: buf,
}, nil
}
示例13: unpackExampleProject
// Attempts to extract a compressed project into the given destination.
func unpackExampleProject(root string) (err error) {
var stat os.FileInfo
// Validating destination.
if stat, err = os.Stat(root); err != nil {
return err
}
if stat.IsDir() == false {
return fmt.Errorf("Expecting a directory.")
}
// Creating a tarbz2 reader.
tbz := tar.NewReader(bzip2.NewReader(bytes.NewBuffer(compressedProject)))
// Extracting tarred files.
for {
hdr, err := tbz.Next()
if err != nil {
if err == io.EOF {
break
}
panic(err.Error())
}
// See http://en.wikipedia.org/wiki/Tar_(computing)
filePath := root + pathSeparator + hdr.Name
switch hdr.Typeflag {
case '0':
// Normal file
fp, err := os.Create(filePath)
if err != nil {
return err
}
io.Copy(fp, tbz)
err = os.Chmod(filePath, os.FileMode(hdr.Mode))
if err != nil {
return err
}
fp.Close()
case '5':
// Directory
os.MkdirAll(filePath, os.FileMode(hdr.Mode))
default:
// fmt.Printf("--> %s, %d, %c\n", hdr.Name, hdr.Mode, hdr.Typeflag)
panic(fmt.Sprintf("Unhandled tar type: %c in file: %s", hdr.Typeflag, hdr.Name))
}
}
return nil
}
示例14: TestWriter_Write12MiB
// Test creating a writer, writing a significant amount of data to it, and
// closing it again.
func TestWriter_Write12MiB(t *testing.T) {
// Generate a sequence of data.
uncompressedStrideIn := make([]byte, 3*1024*1024)
for i := 0; i < len(uncompressedStrideIn); i++ {
uncompressedStrideIn[i] = byte(i % 256)
}
// Test with varying block sizes.
for blockSize100k := 1; blockSize100k <= 9; blockSize100k++ {
// Compress the data.
compressed := AssertWriterWrites(t, blockSize100k, [][]byte{
uncompressedStrideIn,
uncompressedStrideIn,
uncompressedStrideIn,
uncompressedStrideIn,
})
// Decompress the data and verify equality.
uncompressedOut, err := ioutil.ReadAll(bzip2.NewReader(bytes.NewBuffer(compressed)))
if err != nil {
t.Fatalf("Failed to decompress compressed bzip2 data: %v", err)
}
if len(uncompressedOut) != 4*len(uncompressedStrideIn) {
t.Fatalf("Expected decompressed data to be %d B but it is %d B", 4*len(uncompressedStrideIn), len(uncompressedOut))
}
for i := 0; i < 4; i++ {
if !bytes.Equal(uncompressedOut[i*len(uncompressedStrideIn):(i+1)*len(uncompressedStrideIn)], uncompressedStrideIn) {
t.Fatalf("Decompressed data does not match original data")
}
}
}
}
示例15: main
func main() {
flag.Parse()
if *file == "" {
log.Fatal("You must supply a bz2 dump file.")
}
session, err := mgo.Dial(*dburl)
if err != nil {
panic(err)
}
f, err := os.Open(*file)
if err != nil {
log.Fatalf("Error opening file: %v", err)
}
defer f.Close()
z := bzip2.NewReader(f)
p, err := wikiparse.NewParser(z)
if err != nil {
log.Fatalf("Error setting up new page parser: %v", err)
}
err = session.DB(*dbname).C(*collection).EnsureIndex(titleIndex)
if err != nil {
log.Fatal("Error creating title index", err)
}
processDump(p, session.DB(*dbname))
}