Zipping folder containing subfolder using JavaScript - javascript

how to zip a folder using javascript
knowing that i am making a javascript win8 store app , i have access to MyDocumnets on the client PC ,
my task now is to zip a folder (saved data of the app) and upload it to my server ??

There is no direct way to zip a folder, or even a file for that matter, using plain JavaScript (no custom libraries). This is because JavaScript is generally limited to operating in the DOM. It would be really bad for example if some malicious website was able to get you to open a page with their JavaScript on it which say, deleted random files on your machine.
However, you do have an option or two. The closest thing that's available right now (again, not considering custom libraries) is the HTML 5 File API. Support for it across major browsers varies, but most newer builds have at least limited, if not full support for it.
With the HTML 5 File API you can access, edit, and write files in a sandboxed environment in the browser. Based on this, what you could do is:
Get access to all the files you want to put in the zip using the HTML 5 File API
Write all the data from those files into a new file, using the .zip format
Save that file as myfile.zip
Part 2 there is going to be your struggle. Parts 1 and 3 are pretty straight forward. Check out this guide on getting started with HTML 5 File API.
The other, likely better option is to zip the files on your server and then just send the .zip to the client machine. Likely will be a lot easier than using the HTML 5 File API.

An alternative to finding a straight-up JS library (which is difficult), is to find a good C/C++ library and wrap it inside a Windows Runtime component. To give a little quick background, the entirety of the WinRT API is written in a way that makes it possible to project that API surface area into multiple languages such as C++, C#, VB, and JavaScript. This model is extensible, which means that you can write your own APIs that behave in exactly the same way. Visual Studio even has templates for this in the C#, VB, and C++ language options.
For an app written in JavaScript, what this makes possible is that you can access a whole host of additional APIs that are not native to JavaScript, simply by creating a small WinRT component (it's just a DLL with some metadata) that's implemented in a language like C++ that does have access to many more APIs, e.g. those in Win32, COM, and .NET. The whitelist of such APIs for Windows Store apps can be found on http://msdn.microsoft.com/en-us/library/windows/apps/br205757.aspx.
Generally speaking, for a JS app you want to implement the component in C++ (which is how WinRT is implemented). Although can also do it a bit more easily in C# or VB, you end up loading the whole CLR and taking something on the order of a 45MB memory hit.
So if you can find a good ZIP library for C++, and it uses the whitelisted APIs on the previous link, then it's straightforward to create a WinRT component that presents an interface usable from JavaScript. I'd recommend having the JS app just pass something like the StorageFolder you're looking to compress and have it return the StorageFile to upload. Then you can simply pass that StorageFile to the BackgroundTransfer API (http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.backgroundtransfer.aspx), which you should be using for any significant transfer operation as it will make sure it continues if your app is suspended.
For more details on writing WinRT components, refer to Chapter 18 of my free ebook, Programming Windows Store Apps with HTML, CSS, and JavaScript, 2nd Edition. It also details how to create async APIs which is probably what you want in this case because a zipping operation could take a long time and you don't want to block UI responsiveness.

Related

Is it possible to design GUI with HTML+CSS+JavaScript but it will actually run python script?

I've built a very simple assistant app in python which can do very basic tasks like taking notes, reminding you, stopwatch, timer, web scrape for news feeds etc. tkinter seems confusing and looks oldish to me. On the other hand, css js seems much easier to design gui side and way more elegant looking. Is it possible to design a desktop gui app (may be with electron?) using HTML+CSS+JavaScript but it will run my old python codes?
I've been coding for only two months and i suck at it. Please excuse my newbiness.
TLDR: Simply, i want to make the gui side using HTML+CSS+JavaScript to take user input but then it will run python scripts and shows output in the gui app. Is it possible?
It can't be done, you'd have to make it like a web app (although with local webserver serving python responses)
EDIT:
if you don't mind running it in webbrowser, you can make quite easily webserver, that will evaluate your queries...
The popular form of Javascript or ES6 (which you are talking about) is designed to run in browser, so, the limitations are that it can only make calls via browser, i.e. it cannot directly interact with the OS like python's OS module. This means you will need a web-service in your computer that would run a specific python code and return you the responses, this requires a web-service/web-framework, preferably python's like Django, Flask which will run the python script for you because they can make OS calls on the server machine. I do think other non-python web-services are cacpable to do so, but of course, the natural preference would be 'Python-based services'.
Sidenote:
If the case was with Node.js(i.e. the server-side js) and not ES6(client-side browser-run) you would have an upperhand i.e. you could invoke python scripts on your server because node.js like the python-based web-servers do support os calls.
Kinda, but its real ugly. If you can host your data and whatnot the other approaches will work.
You have to build your project around nw.js. Essentially it is a Chromium build that adds local file system access back in. You can build an HTML+JS Front end and access a node.js backend running in the same thread. Via node you can shellout to call your python program, or run a local python web server.
I built a mapping app that allowed the user to select a local file, process it on the local machine with python and display the results in an interactive D3 app with geojson based layers of the UnitedStates. Since the data was proprietary I could not host it outside the company. Since I was not IT, I could not host it inside the company. nw.js allowed me to package everything into an installer and deploy to other people within the company as a standalone app.
See here for more information:
Official site: http://nwjs.io
Official documentation: http://docs.nwjs.io/
Introduction
NW.js is an app runtime based on Chromium and node.js. You can write native apps in HTML and JavaScript with NW.js. It also lets you call Node.js modules directly from the DOM and enables a new way of writing native applications with all Web technologies.
It was created in the Intel Open Source Technology Center.
Features
Apps written in modern HTML5, CSS3, JS and WebGL.
Complete supportfor Node.js APIs and all its third party modules.
Good performance: Node and WebKit run in the same thread: Function
calls are made straightforward; objects are in the same heap and
can just reference each other;
Easy to package and distribute apps. Available on Linux, Mac OS X
and Windows

Portable Javascript Application with String to File Output

I am using Javascript wrapped in HTML to simplify the task of one of my students. Her task is to create a text file for a research project, which will act as a configuration file for the analysis software.
I decided on Javascript, because I wanted portable, transparent code, with zero dependencies (no libs, no server, no installation), yet a familiar feel from the web that is easy to get started with. However, client side Javascript appears to have its limits when it comes to handling file output.
There are multiple questions and answers on Stackoverflow that address the issue by appealing to server-side solutions, external dependencies, and the newer HTML5 download element.
I have considered if I should use a complementary script or batch file that reads the output, but I am not sure about how to best implement such a layer. The file is complex to generate and this is achieved using form elements.
Another idea would be to package the script as an executable. For example, a browser could be called from Java, or the HTML/JS could be converted somehow. Perhaps there is a wrapper that I am not familiar with.
This is one of those side projects that is fast to code and so I would assume that there is a go-to solution among programmers for this type of problem. On the one hand, this is a packaging problem. On the other hand, it is about some of the limitations with Javascript for projects that run without a server backbone.
How can I deliver a no-bells-and-whistles Javascript application that is local only and capable of handling file I/O?

How to setup a web app which can handle local data without uploading the data? Use python

I have a small python program to help my colleagues to analyse some tsv data. The data is not big, usually below 20MB. I developed the GUI with PyQT. I want to change this desktop program to a web app so my colleagues don't have to upgrade the program every time I change it or fix a bug. They can just go to a website and use Chrome as the GUI.
So how do I do this? I spend most of my time developing desktop program and just know some basic web developing knowledges. I have read some framework such as flask and web2py, and know how to use HTML and Javascript to make buttons but no clue to achieve my purpose.
Can someone give me a practical way to do this?
It'd be better if the user don't have to upload the local data to the server. Maybe just download the python code from server then execute in Chrome. Is this possible?
Thanks.
No, you cannot run Python code in a web browser.[1] You'd have to port the core of your application to JavaScript to do it all locally.
Just do the upload. 20MB isn't all that much data, and if it's stored on the server then they can all look at each others' results, too.
[1] There are some tools that try to transpile Python to JavaScript: pyjs compiles directly, and Emscripten is an entire LLVM interpreter in JS that can run CPython itself. I wouldn't really recommend relying on these.
The whole point of a web application is that the GUI is written in HTML, CSS, and JavaScript, not Python. However, it talks to a web service, which can be written in Python.
For a well-written desktop app, the transition should be pretty easy. If you've already got a clean separation between the GUI part and the engine part of your code, the engine part will only need minor changes (and maybe stick it behind, e.g., a WSGI server). You will have to rewrite the GUI part for the web, but in a complex app, that should be the easy part.
However, many desktop GUI apps don't have such a clean separation. For example, if you have button handlers that directly do stuff to your model, there's really no way to make that work without duplicating the model on both sides (the Python web service and the JS client app) and synchronizing the two, which is a lot of work and leads to a bad experience. In that case, you have to decide between rewriting most of your app from scratch, or refactoring it to the point where you can web-service-ify it.
If you do choose to go the refactoring route, I'd consider adding a second local interface (maybe using the cmd module to build a CLI, or tkinter for an alternate GUI), because that's much easier to do. Once the same backend code can support your PyQt GUI and your cmd GUI, adding a web interface is much easier.
If I get your point correctly, you want
Web connection, so your python program updated on server, client get it before using it.
Data store on local to avoid upload big file.
You can write a python program to check a server location to get your latest program if needed. You need a url / server file for program version / created date/time information to determine if you need to update or not.
After get latest python program, then start this python program to run locally.
With this said, What you need is to update your program to add below features:
Access your server, to get latest version information
Check against current version to see if you need to download latest program
Download latest version and use that to run locally.
Does this solve your problem?

How to pass data from C++ application to Javascript

I'm developing a jQuery-based pivot table.
The goal is not a web app but a desktop-based application (C++). In my idea, data is retrieved by the application from the database, then passed to a html page and then showed through the pivot-table plugin.
There is no web server and the web page containing the plugin cannot access to the database.
So, how to pass data in an efficient way? I've seen other questions here on SO around this matter, but I think we're in a different scenario. Of course I can write the data in a txt/xml/js file, but I've experienced that for huge amounts of data, writing down files is costing a lot.
If you don't want to add support for the http protocol to your application, and you don't want to write to additional files, then IMO your best bet is to create a wrapping http server for your application. Then the javascript page can access the running wrapper which can talk to your "real" application.
You could create such a server relatively easily in python using the twisted framework, ruby using rails and the bundled webrick server, or the v8 Javascript engine node.js. (I'm sure there's dozens of other options out there too)
Which of there would be best for you will depend a lot on which languages you have experience with and what your deployment requirements are (supported OSes, existing installed applications, installation size, license terms on your software etc.)
Do you have a specific framework for your desktop app ? If you use Qt for the GUI, you can also integrate javascript quite easily.
http://efforts.embedded.ufcg.edu.br/qt/?p=84
You might be able to use named pipes to pass data to a static page.
It might also be better to just make your c++ program into a simple web server which opens a port, and have it generate the web page when the user goes to http://localhost:8080.

Advice on starting JS or JQuery for file processing

My knowledge on web technologies (JS, JQ) are limited and I want to start learning them. As a starting point I want to do some file processing. Because it is something I have to do for my work and was planning to do it in Java. What I basically need to do is to go through a list of text files (assembly files) in a folder and search for routines and then list them. This is the first step and is a trivial task in Java.
But I wanted to take this a step further and do it in the browser, so that others in my team also can use it without installing anything (and also to impress them a little bit in the process. since I'm the new guy in the team :-)).
So when I input the folder, the script will go through the files and search and will display results in a web page. Basically first page will be a list of files in the folder, and clicking a file name will take me to another page which displays the routines in that file.
Sorry to bother you with details, but what I actually want to know are:
Is this possible with JS? (to
search for text patterns in a file)
Should I start with JS or JQ? (I
think many would recommend starting
with JS, but since this is a side
project and this is done purely in
my own time, would you suggest start
learning JQ because it's relatively
simpler to learn (from what I have
read) for a beginner?
Or should I just do the processing
in JAva and then interface the
results to a webpage
Any advice is appreciated.
Thank you very much.
Java and JavaScript have nothing to do with each other, jQuery is library written to simplify usage of JavaScript with some handy shortcuts.
I'm afraid JavaScript would not be able to parse text files as its main usage is manipulating content inside browser window and limited by different security policies.
To parse files you have to chose server side language.
maybe you can use java to deal with the file processing, and then send the result to js script , which will show these results to users.
js's ability is limited
For security reasons, JavaScript is sandboxed within the browser, and has basically no access to the local file system. From what you have described, it sounds like your best option is to use Java to process ...whatever...
This function has nothing to do with web browsing. Why is a browser the best tool for the job, anyway?

Categories

Resources