Search code examples
firefox-addonfirefox-addon-sdk

Making zip file in Firefox plugin


I developed a firefox plugin that store some data in plugin folder. How can I add a backup button, so when user click on it, a save dialog displayed and the folder be packed in a zip file and be stored in user's desired location?


Solution

  • As it is, the Add-on SDK doesn't have any modules to write ZIP files. However, with chrome authority you can access the nsIZipWriter interface of the underlying platform. For example:

    var {Cc, Ci, Cu} = require("chrome");
    var {FileUtils} = Cu.import("resource://gre/modules/FileUtils.jsm");
    
    var zipFile = FileUtils.getFile("ProfD", ["test.zip"], false);
    var savedFile = new FileUtils.File("C:\\foo.exe");
    var zipWriter = Cc["@mozilla.org/zipwriter;1"].createInstance(Ci.nsIZipWriter);
    zipWriter.open(zipFile, FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE);
    zipWriter.addEntryFile("foo.exe", zipWriter.COMPRESSION_BEST, savedFile, false);
    zipWriter.close();
    

    This will create a test.zip file in the user's profile directory and write c:\foo.exe into it. See also FileUtils documentation.