Touffy / client-zip

A client-side streaming ZIP generator
MIT License
342 stars 22 forks source link

How to convert .txt to .zip file from local storage path #4

Closed pratiksan45 closed 4 years ago

pratiksan45 commented 4 years ago

Hi. I want to convert a text file (./assets/file.txt) into a zip file and then store it in the exact same folder. Can you tell me how I can do it .

Touffy commented 4 years ago

This has nothing to do with LocalStorage, right? and you are not trying to do something that you should be doing in Node.js, which can access the native ZIP process and has no restrictions on local filesystem access?

Using the experimental Files and Directories API, you should be able to pick a specific File if the user allows it. But that is beyond the scope of the client-zip library. This library can only help you once you have the File.

At that point, just call downloadZip([file]) to get a Response containing the zipped file.

Now you're stuck, because the Files and Directories API does not currently allow writing or removing anything to/from the filesystem, only reading. If it did, you would simply create a new file called "file.zip", get a FileWriter on it, and pipe the Response into the writer. Again, the ability to write to the local filesystem is not something that client-zip can help you with. Sorry.

pratiksan45 commented 4 years ago

Can I save the zip file using this client zip instead of download ?

Touffy commented 4 years ago

client-zip only gives you a Response with a ZIP file as its body. What you do with it is not in the scope of the library. I believe a Response is versatile enough (given it has .body and .blob() and .arrayBuffer() etc.) for all possible use cases. Saving the Response as a file in the user's local filesystem may not be possible at all or experimental, while making the user download it is very simple (that's why the example in the README creates a download link). Either way, client-zip does not and will not go beyond creating the Response (even the blob() method in the example is a native method of Responses, not something specific to this library).

I don't want to discourage you, but I am closing this issue because it is definitely out-of-scope for client-zip. I think you should look up how to save files to the local filesystem. I've already given you a pointer in that direction, but you haven't provided enough context to know if it's even relevant to you (for example, if you are writing a hybrid application, you will have non-standard APIs to access the filesystem).

pratiksan45 commented 4 years ago

Hi @Touffy , Actually I am using this client-zip to generate a zip file and share the zip file using https://github.com/moberwasserlechner/capacitor-filesharer Filesharer plugin. In here, I am converting the blob zip file I am getting from downloadZip() into base64 data to further use it in my Filesharer plugin but I am doing this with a sole purpose of compressing the file size, but you just said that this plugin dont compress the file.

Touffy commented 4 years ago

A base64 string for the whole archive ? that is so wrong… you should tell the capacitor-filesharer maintainers we're not in 2005 anymore.

Sorry, I digress. I see why you think you need compression there. Like I said before, I think the best option for you would be to take advantage of the access you have to non-standard platform APIs (in this case, Capacitor-Zip looks like a good candidate). Even if client-zip supported compression, it would be slower.

However, if I understand correctly what capacitor-filesharer does, it looks like nothing more than clicking a regular link to trigger a download (except maybe without the requirement for user interaction). Does the user's device keep the file zipped or does it unzip it automatically ? if the latter, compression would be a waste of CPU.