mafintosh / tar-stream

tar-stream is a streaming tar parser and generator.
MIT License
408 stars 92 forks source link

How to read sub folder/files ? #28

Closed yanzou closed 9 years ago

yanzou commented 9 years ago

Hi, Sorry to raise issue here but I think you guys maintaining this module would understand this best, So I thought I might get some help from here.

I am using this module to read a .tgz file, and to read every files's content from this tar file. I am kind of stuck here

here is what I am trying to do :

.tgzfile structure:

root_folder
|-- _sub_folder1
|        |-- file1
|        |-- file2
|        ....
|-- _sub_folder2
...

(in coffee script) Read every sub folder and file

extract = require('tar-stream').extract()
fs.createReadStream(FILE_PATH).pipe(zlib.createUnzip()).pipe(extract)
    .on 'entry', (header, stream, callback) ->
        console.log "header -->", header.name, header.size, header.type
        if hearder.type == "directory"
             #go inside this directory and find all files
             #read content of every file......
             #           what should I do here ??
        else if hearder.type == "file"
             #read content of  this  file......

        stream.resume()
    .on 'error', ->
        console.log "error"
    .on 'finish', ->
        console.log "finished"

out put:

header --> offline_2014-08-06_16:54:28/ 0 directory
this entry end
mafintosh commented 9 years ago

tar-stream parses every entry in the tar file. when parsing the tar file you visit every file/directory try doing something like this:

fs.createReadStream(FILE_PATH).pipe(zlib.createUnzip()).pipe(extract)
  .on('entry', function(header, stream, callback) {
    console.log(header.name)
    stream.resume()
    stream.on('end', function() {
      callback()
    })    
  })

this should print out all entries in the tar ball. does that make sense?

mafintosh commented 9 years ago

checkout tar-fs if you are looking for something that extracts the entire tar ball to your filesystem

yanzou commented 9 years ago

Thanks :+1: