Giter Site home page Giter Site logo

Comments (13)

dduponchel avatar dduponchel commented on August 26, 2024

I personally like the idea :)
I can work on it, if Stuk is interested by this feature.
If we add this, jszip API will need to change a bit : the "load-modify-generate" workflow requires more control on the included files (a get method ? ability to change directly a file ?).

from jszip.

flying-sheep avatar flying-sheep commented on August 26, 2024

currently we have this constructor:

JSZip(compression_method) or JSZip()

i propose the change:

JSZip(data_string_or_compression_method) or JSZip()

It sniffs what the string is (looks for magic number or if it is one of the supported compression methods or something), and if it detects zipped data, it reads the compression method from the file and creates the JSZip object. Else it creates an empty JSZip object with the specified or default compression method.


also currently we have these “public” methods:

  • .add(name, data, options)
    returns JSZip object this was called upon
  • .folder(name)
    returns child-JSZip object with the folder as root
  • .find(needle)
    returns an arrow of matching objects of the type {name: string, data: string, dir: true/false}
  • .remove(needle)
    recursively removes matching files/folders
    returns JSZip object this was called upon
  • .generate(asBytes)
    returns zip file from data.

i love this api, so i’d like to keep most of it. my proposed change is the following:

merge .find/.add and .find/.folder (eliminate find and rename add to file):

  • calling .file(path: string) returns the corresponding file,
    calling .file(path: regex) returns an array of matching files (like find does now),
    calling .file(path: string, data) or .file(path: string, data, options) adds the file to the matching folder, generating folders on the fly if needed, and returns the JSZip object it was called upon for chaining (kinda like add does now)
  • calling .folder(path: string) creates the folder and returns a JSZip object with the generated folder as root, which is linked to the JSZip object it was called upon (like folder does now)
    calling .folder(needle: regex) creates a list of JSZip objects that correspond to existing folders that match the regex, without creating new ones.

the file options would maybe contain a “force replacement” option. apart from that we don’t need much, except maybe a shortcut for .file(/[^/]+/) for finding all first-level files, e.g. .file()… but what do you think? maybe we should handle it like jquery, where each file list and folder list is an JSZip object itself.

from jszip.

dduponchel avatar dduponchel commented on August 26, 2024

JSZip(data_string_or_compression_method) or JSZip()

I don't think trying to auto-detect the input is a good idea (same type (string) for compression method and data string).
The compression method should be only needed when generating the zip, so I think the generate() method is a better place. The compression currently occurs when adding a file but we should change that : giving access to a compressed content is not really useful.
What do you think of generate(asBytes) becoming generate(options) ? Where default options are :

{
    asBytes : false,
    compression : 'STORE'
}

The rest of your proposition sounds good to me !

from jszip.

flying-sheep avatar flying-sheep commented on August 26, 2024

you are of course right about generate being the better place. that’s the way to go.

the autodetection would work nicely, though. we’d just have to sniff the string.

from jszip.

Stuk avatar Stuk commented on August 26, 2024

These are all absolutely great ideas, and I love the direction going on here. If anyone fancies hacking along these lines I will happily welcome the changes. I'm going to see if I can get some spare time this week to start bringing things up to date.

from jszip.

dduponchel avatar dduponchel commented on August 26, 2024

I should finish the code/doc/tests for the new API this week end :)

from jszip.

flying-sheep avatar flying-sheep commented on August 26, 2024

awesome!

from jszip.

dduponchel avatar dduponchel commented on August 26, 2024

You can see the updated doc at http://dduponchel.github.com/jszip/ and the code on my branch develop. I'm waiting your feedback !

from jszip.

Stuk avatar Stuk commented on August 26, 2024

Cheers! I'm in the middle of moving house/country at the moment, so it will take me a while to get a moment look at the code, sorry!

from jszip.

flying-sheep avatar flying-sheep commented on August 26, 2024

hi, i have some problems with it. i tested it with jquery’s ajax function via python’s SimpleHTTPServer (to get around possible permission issues with local files). i used the following code:

$.ajax({
    url: "packs/johnsmith_v8.4.zip",
    dataType: "text",
    success: function(data){
        var testZip = new JSZip(data);
        console.log(testZip.file("terrain.png"));
    }
});

and this zip file (saved locally)

i get the error

End of stream reached (stream length = 4044001, asked index = 4324726). Corrupted zip ?

but zipinfo -v says:

Zip archive file size:                   4334859 (000000000042250Bh)
Actual end-cent-dir record offset:       4334837 (00000000004224F5h)
Expected end-cent-dir record offset:     4334837 (00000000004224F5h)

what cuts the stream? is it some kind of encoding issue?

PS: i tried with some more .zips, but to no avail.

from jszip.

dduponchel avatar dduponchel commented on August 26, 2024

Hi,

That's a known issue : the browser tries to parse the ajax response as text and break it (issue #6).

The solution for firefox/chrome/opera is to use
mimeType:'text/plain; charset=x-user-defined'
instead of
dataType: "text"

The easiest way to test in IE might be to add a test case in test/index.html : the zip files are now (on my branch develop) loaded via ajax calls.

I should add a note in the documentation as the issue is not obvious !

from jszip.

flying-sheep avatar flying-sheep commented on August 26, 2024

sorry for not looking into other issues blush. thanks to you it works fine now:

$.ajaxSetup({
    mimeType: "text/plain; charset=x-user-defined",
    error: function(req, status, error) { console.error(error); }
});

from jszip.

Stuk avatar Stuk commented on August 26, 2024

Now implemented thanks to the above pull request.

from jszip.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.