OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package tar | 5 package tar |
6 | 6 |
7 // TODO(dsymonds): | 7 // TODO(dsymonds): |
8 // - pax extensions | 8 // - pax extensions |
9 | 9 |
10 import ( | 10 import ( |
11 "bytes" | 11 "bytes" |
12 "io" | 12 "io" |
13 "os" | 13 "os" |
14 "strconv" | 14 "strconv" |
15 ) | 15 ) |
16 | 16 |
17 var ( | 17 var ( |
18 HeaderError os.Error = os.ErrorString("invalid tar header") | 18 HeaderError os.Error = os.ErrorString("invalid tar header") |
19 ) | 19 ) |
20 | 20 |
21 // A Reader provides sequential access to the contents of a tar archive. | 21 // A Reader provides sequential access to the contents of a tar archive. |
22 // A tar archive consists of a sequence of files. | 22 // A tar archive consists of a sequence of files. |
23 // The Next method advances to the next file in the archive (including the first
), | 23 // The Next method advances to the next file in the archive (including the first
), |
24 // and then it can be treated as an io.Reader to access the file's data. | 24 // and then it can be treated as an io.Reader to access the file's data. |
25 // | 25 // |
26 // Example: | 26 // Example: |
27 //» tr := tar.NewReader(r); | 27 //» tr := tar.NewReader(r) |
28 // for { | 28 // for { |
29 //» » hdr, err := tr.Next(); | 29 //» » hdr, err := tr.Next() |
30 // if err != nil { | 30 // if err != nil { |
31 // // handle error | 31 // // handle error |
32 // } | 32 // } |
33 // if hdr == nil { | 33 // if hdr == nil { |
34 // // end of tar archive | 34 // // end of tar archive |
35 // break | 35 // break |
36 // } | 36 // } |
37 //» » io.Copy(data, tr); | 37 //» » io.Copy(data, tr) |
38 // } | 38 // } |
39 type Reader struct { | 39 type Reader struct { |
40 r io.Reader | 40 r io.Reader |
41 err os.Error | 41 err os.Error |
42 nb int64 // number of unread bytes for current file entry | 42 nb int64 // number of unread bytes for current file entry |
43 pad int64 // amount of padding (ignored) after current file entry | 43 pad int64 // amount of padding (ignored) after current file entry |
44 } | 44 } |
45 | 45 |
46 // NewReader creates a new Reader reading from r. | 46 // NewReader creates a new Reader reading from r. |
47 func NewReader(r io.Reader) *Reader { return &Reader{r: r} } | 47 func NewReader(r io.Reader) *Reader { return &Reader{r: r} } |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 } | 217 } |
218 n, err = tr.r.Read(b) | 218 n, err = tr.r.Read(b) |
219 tr.nb -= int64(n) | 219 tr.nb -= int64(n) |
220 | 220 |
221 if err == os.EOF && tr.nb > 0 { | 221 if err == os.EOF && tr.nb > 0 { |
222 err = io.ErrUnexpectedEOF | 222 err = io.ErrUnexpectedEOF |
223 } | 223 } |
224 tr.err = err | 224 tr.err = err |
225 return | 225 return |
226 } | 226 } |
OLD | NEW |