Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit e5e9e6d

Browse files
authored
Merge pull request #14 from YtvwlD/bzip2
Support bzip2
2 parents 5b08c9b + fa6c8d3 commit e5e9e6d

12 files changed

Lines changed: 259 additions & 2 deletions

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ Supported file extensions:
1111
* .tar
1212
* .tar.gz
1313
* .tgz
14+
* .tar.bz2
15+
* .tbz
16+
* .tbz2
1417
* .war
1518
* .zip
1619
* .egg
@@ -69,6 +72,17 @@ Buffer contents of the uncompressed paths. The `callback` gets two arguments
6972
`callback` - The function to call after reading completes with an error or
7073
the Buffer contents.
7174

75+
### archive.readBzip(bzipArchivePath, callback)
76+
77+
Read the contents of the bzipped archive path and invoke the callback with the
78+
Buffer contents of the uncompressed paths. The `callback` gets two arguments
79+
`(error, pathContents)`.
80+
81+
`bzipArchivePath` - The string path to the bzipped archive file.
82+
83+
`callback` - The function to call after reading completes with an error or
84+
the Buffer contents.
85+
7286
### ArchiveEntry
7387

7488
Class representing a path entry inside an archive file.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"async": "~0.2.9",
5555
"colors": "~0.6.2",
5656
"rimraf": "~2.2.6",
57+
"unbzip2-stream": "^1.3.3",
5758
"yauzl": "^2.9.1"
5859
}
5960
}

spec/bzip-spec.coffee

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
archive = require '../lib/ls-archive'
2+
path = require 'path'
3+
4+
describe "bzipped tar files", ->
5+
fixturesRoot = null
6+
7+
beforeEach ->
8+
fixturesRoot = path.join(__dirname, 'fixtures')
9+
10+
describe ".list()", ->
11+
describe "when the archive file exists", ->
12+
it "returns files in the bzipped tar archive", ->
13+
bzipPaths = null
14+
callback = (error, paths) -> bzipPaths = paths
15+
archive.list(path.join(fixturesRoot, 'one-file.tar.bz2'), callback)
16+
waitsFor -> bzipPaths?
17+
runs ->
18+
expect(bzipPaths.length).toBe 1
19+
expect(bzipPaths[0].path).toBe 'file.txt'
20+
expect(bzipPaths[0].isDirectory()).toBe false
21+
expect(bzipPaths[0].isFile()).toBe true
22+
expect(bzipPaths[0].isSymbolicLink()).toBe false
23+
24+
it "returns files in the bzipped tar archive", ->
25+
bzipPaths = null
26+
callback = (error, paths) -> bzipPaths = paths
27+
archive.list(path.join(fixturesRoot, 'one-file.tbz'), callback)
28+
waitsFor -> bzipPaths?
29+
runs ->
30+
expect(bzipPaths.length).toBe 1
31+
expect(bzipPaths[0].path).toBe 'file.txt'
32+
expect(bzipPaths[0].isDirectory()).toBe false
33+
expect(bzipPaths[0].isFile()).toBe true
34+
expect(bzipPaths[0].isSymbolicLink()).toBe false
35+
36+
it "returns files in the bzipped tar archive", ->
37+
bzipPaths = null
38+
callback = (error, paths) -> bzipPaths = paths
39+
archive.list(path.join(fixturesRoot, 'one-file.tbz2'), callback)
40+
waitsFor -> bzipPaths?
41+
runs ->
42+
expect(bzipPaths.length).toBe 1
43+
expect(bzipPaths[0].path).toBe 'file.txt'
44+
expect(bzipPaths[0].isDirectory()).toBe false
45+
expect(bzipPaths[0].isFile()).toBe true
46+
expect(bzipPaths[0].isSymbolicLink()).toBe false
47+
48+
it "returns folders in the bzipped tar archive", ->
49+
bzipPaths = null
50+
callback = (error, paths) -> bzipPaths = paths
51+
archive.list(path.join(fixturesRoot, 'one-folder.tar.bz2'), callback)
52+
waitsFor -> bzipPaths?
53+
runs ->
54+
expect(bzipPaths.length).toBe 1
55+
expect(bzipPaths[0].path).toBe 'folder'
56+
expect(bzipPaths[0].isDirectory()).toBe true
57+
expect(bzipPaths[0].isFile()).toBe false
58+
expect(bzipPaths[0].isSymbolicLink()).toBe false
59+
60+
it "returns folders in the bzipped tar archive", ->
61+
bzipPaths = null
62+
callback = (error, paths) -> bzipPaths = paths
63+
archive.list(path.join(fixturesRoot, 'one-folder.tbz'), callback)
64+
waitsFor -> bzipPaths?
65+
runs ->
66+
expect(bzipPaths.length).toBe 1
67+
expect(bzipPaths[0].path).toBe 'folder'
68+
expect(bzipPaths[0].isDirectory()).toBe true
69+
expect(bzipPaths[0].isFile()).toBe false
70+
expect(bzipPaths[0].isSymbolicLink()).toBe false
71+
72+
it "returns folders in the bzipped tar archive", ->
73+
bzipPaths = null
74+
callback = (error, paths) -> bzipPaths = paths
75+
archive.list(path.join(fixturesRoot, 'one-folder.tbz2'), callback)
76+
waitsFor -> bzipPaths?
77+
runs ->
78+
expect(bzipPaths.length).toBe 1
79+
expect(bzipPaths[0].path).toBe 'folder'
80+
expect(bzipPaths[0].isDirectory()).toBe true
81+
expect(bzipPaths[0].isFile()).toBe false
82+
expect(bzipPaths[0].isSymbolicLink()).toBe false
83+
84+
describe "when the archive path does not exist", ->
85+
it "calls back with an error", ->
86+
archivePath = path.join(fixturesRoot, 'not-a-file.tar.bz2')
87+
pathError = null
88+
callback = (error) -> pathError = error
89+
archive.list(archivePath, callback)
90+
waitsFor -> pathError?
91+
runs -> expect(pathError.message.length).toBeGreaterThan 0
92+
93+
describe "when the archive path isn't a valid bzipped tar file", ->
94+
it "calls back with an error", ->
95+
archivePath = path.join(fixturesRoot, 'invalid.tar.bz2')
96+
pathError = null
97+
callback = (error) -> pathError = error
98+
archive.list(archivePath, callback)
99+
waitsFor -> pathError?
100+
runs -> expect(pathError.message.length).toBeGreaterThan 0
101+
102+
describe "when the second to last extension isn't .tar", ->
103+
it "calls back with an error", ->
104+
archivePath = path.join(fixturesRoot, 'invalid.txt.bz2')
105+
pathError = null
106+
callback = (error, contents) -> pathError = error
107+
archive.list(archivePath, callback)
108+
waitsFor -> pathError?
109+
runs -> expect(pathError.message.length).toBeGreaterThan 0
110+
111+
describe ".readFile()", ->
112+
describe "when the path exists in the archive", ->
113+
it "calls back with the contents of the given path", ->
114+
archivePath = path.join(fixturesRoot, 'one-file.tar.bz2')
115+
pathContents = null
116+
callback = (error, contents) -> pathContents = contents
117+
archive.readFile(archivePath, 'file.txt', callback)
118+
waitsFor -> pathContents?
119+
runs -> expect(pathContents.toString()).toBe 'hello\n'
120+
121+
it "calls back with the contents of the given path", ->
122+
archivePath = path.join(fixturesRoot, 'one-file.tbz')
123+
pathContents = null
124+
callback = (error, contents) -> pathContents = contents
125+
archive.readFile(archivePath, 'file.txt', callback)
126+
waitsFor -> pathContents?
127+
runs -> expect(pathContents.toString()).toBe 'hello\n'
128+
129+
it "calls back with the contents of the given path", ->
130+
archivePath = path.join(fixturesRoot, 'one-file.tbz2')
131+
pathContents = null
132+
callback = (error, contents) -> pathContents = contents
133+
archive.readFile(archivePath, 'file.txt', callback)
134+
waitsFor -> pathContents?
135+
runs -> expect(pathContents.toString()).toBe 'hello\n'
136+
137+
describe "when the path does not exist in the archive", ->
138+
it "calls back with an error", ->
139+
archivePath = path.join(fixturesRoot, 'one-file.tar.bz2')
140+
pathError = null
141+
callback = (error, contents) -> pathError = error
142+
archive.readFile(archivePath, 'not-a-file.txt', callback)
143+
waitsFor -> pathError?
144+
runs -> expect(pathError.message.length).toBeGreaterThan 0
145+
146+
describe "when the archive path does not exist", ->
147+
it "calls back with an error", ->
148+
archivePath = path.join(fixturesRoot, 'not-a-file.tar.bz2')
149+
pathError = null
150+
callback = (error, contents) -> pathError = error
151+
archive.readFile(archivePath, 'not-a-file.txt', callback)
152+
waitsFor -> pathError?
153+
runs -> expect(pathError.message.length).toBeGreaterThan 0
154+
155+
describe "when the archive path isn't a valid bzipped tar file", ->
156+
it "calls back with an error", ->
157+
archivePath = path.join(fixturesRoot, 'invalid.tar.bz2')
158+
pathError = null
159+
callback = (error, contents) -> pathError = error
160+
archive.readFile(archivePath, 'invalid.txt', callback)
161+
waitsFor -> pathError?
162+
runs -> expect(pathError.message.length).toBeGreaterThan 0
163+
164+
describe "when the second to last extension isn't .tar", ->
165+
it "calls back with an error", ->
166+
archivePath = path.join(fixturesRoot, 'invalid.txt.bz2')
167+
pathError = null
168+
callback = (error, contents) -> pathError = error
169+
archive.readFile(archivePath, 'invalid.txt', callback)
170+
waitsFor -> pathError?
171+
runs -> expect(pathError.message.length).toBeGreaterThan 0
172+
173+
describe ".readBzip()", ->
174+
it "calls back with the string contents of the archive", ->
175+
archivePath = path.join(fixturesRoot, 'file.txt.bz2')
176+
archiveContents = null
177+
callback = (error, contents) -> archiveContents = contents
178+
archive.readBzip(archivePath, callback)
179+
waitsFor -> archiveContents?
180+
runs -> expect(archiveContents.toString()).toBe 'hello\n'
181+
182+
describe "when the archive path isn't a valid bzipped tar file", ->
183+
it "calls back with an error", ->
184+
archivePath = path.join(fixturesRoot, 'invalid.tar.bz2')
185+
readError = null
186+
callback = (error, contents) -> readError = error
187+
archive.readBzip(archivePath, callback)
188+
waitsFor -> readError?
189+
runs -> expect(readError.message.length).toBeGreaterThan 0
190+
191+
describe "when the archive path does not exist", ->
192+
it "calls back with an error", ->
193+
archivePath = path.join(fixturesRoot, 'not-a-file.tar.bz2')
194+
readError = null
195+
callback = (error, contents) -> readError = error
196+
archive.readBzip(archivePath, callback)
197+
waitsFor -> readError?
198+
runs -> expect(readError.message.length).toBeGreaterThan 0

spec/fixtures/file.txt.bz2

42 Bytes
Binary file not shown.

spec/fixtures/one-file.tar.bz2

135 Bytes
Binary file not shown.

spec/fixtures/one-file.tbz

135 Bytes
Binary file not shown.

spec/fixtures/one-file.tbz2

135 Bytes
Binary file not shown.

spec/fixtures/one-folder.tar.bz2

126 Bytes
Binary file not shown.

spec/fixtures/one-folder.tbz

126 Bytes
Binary file not shown.

spec/fixtures/one-folder.tbz2

126 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)