sohonetlabs / tarfile.js

A JavaScript library to load local and remote tar files from tar archives and json_packed files
2 stars 1 forks source link

Read just one file #1

Open fobetor opened 10 years ago

fobetor commented 10 years ago

Hello, I'm not a programmer, so I cant read the code easyli, perhaps you can help me. I want to tar several images in a file, with their directory sturcture and then, just read a file referenced with a directory path from the the tar file.... ie: DIRA/DIRB/image.jpg Is there a way to read a file in this way? Can you help me with a line of code please? Thank you very much Fernando

mmcardle commented 10 years ago

Hi Fernando,

Are you trying to load a specific file from a tar in javascript? Something like this would work.

 TarFile.load_file(file, function(tf){
    this.files.forEach(function(f) {
        // Do something clever with the file
        if(f.filename=="DIRA/DIRB/image.jpg"){
            // Do something with the image file
        }
    });
 });
fobetor commented 10 years ago

Thanks, I'm testing this.... let me see...

fobetor commented 10 years ago

Scuse me but... with this code: TarFile.load_file('E.tar', function(tf){ this.files.forEach(function(f) { // Do something clever with the file if(f.filename=="3/3/3.png"){ // Do something with the image file var e = document.createElement('div'); document.body.appendChild(e); var p = document.createElement('p'); p.appendChild(document.createTextNode(f.filename + " (" + f.length + " bytes)")); e.appendChild(p); var img = new Image(); img.src = f.toDataURL(); e.appendChild(img); console.log(f); } }); });

I get in firefox console: TypeError: Argument 1 of FileReader.readAsBinaryString is not an object. reader.readAsBinaryString(blob);

Perhaps I'm missing something...?

mmcardle commented 10 years ago

HI Fernando,

You need to load a file from a HTML input element, if you had this INPUT element and where using jQuery

<input type='file' id='file_input' /> 

You could then do

$('#file_input').change(function() {

    var file = $(this)[0].files[0];

    TarFile.load_file(file, function(tf){
          this.files.forEach(function(f) {
           // Do something clever with the file
            if(f.filename=="DIRA/DIRB/image.jpg"){
               // Do something with the image file
           }
        });
    });
});
fobetor commented 10 years ago

Ah... let me get the "big idea"... so...if I want to read a file from within a tar archive in execution time (you know, calling a function that retrieves the file from the tar) is not possible... I need to open the file, or read onto memory the file or similar? This is because of cors and javascript limitations? The problem is that I have a lot of images in a lot of directories, and I was wondering if it was possible to have a tar archive and just read from it (its a map server tiled image) Thank you very much form your time

mmcardle commented 10 years ago

TarFile.load_file reads file from the local filesystem TarFile.load reads files from the server

But it seems pointless to load a whole tarfile of images just to read 1 image? This would require downloading the whole tarfile from the server each time.

Why not just load the required image on demand?

fobetor commented 10 years ago

The objective is to let the user have the hole tar archive in a local file system (a tablet) and read the images from that local tar file "on demand". So the filesystem is not messes up with lots of little images and directories. Since the tar is just a way of storing things without compression.... Ah, the example of "DIRA/DIRB/image.jpg is just to test with one image. It woulfd be a variable name for each call from within javascript

mmcardle commented 10 years ago

Then you might need something like:

var image_files;

TarFile.load(URL_TO_YOUR_TAR_FILE, function(tf){
    image_files = this.files;
});

var some_other_function = function(IMAGE_NAME){
    for(var i=0; i<images_files.length;i++)
        var f = images_files[i];
        if(f.filename==IMAGE_NAME){
           // Do something with the image file

        });
    }
}
fobetor commented 10 years ago

Yes , this is what works. Thanks you very much Best regards Fernando