0

我正在尝试使用以下代码创建一个 zip 文件...我有一个像“D:\smad”这样的文件夹。我有2个文件。我想创建一个名为“smad.zip”的 zip 文件夹。但我找到了一些“将文件添加到 Zip 存档”的代码..这里是这样的

 var file = Components.classes["@mozilla.org/file/directory_service;1"].
       getService(Components.interfaces.nsIProperties).
       get("AChrom", Components.interfaces.nsIFile);

       file.initWithPath(file.path+"\\smad.zip");

 var file11 = Components.classes["@mozilla.org/file/directory_service;1"].
       getService(Components.interfaces.nsIProperties).
       get("AChrom", Components.interfaces.nsIFile);

       file11.initWithPath(file.path+"\\home.txt");


var zipWriter = Components.Constructor("@mozilla.org/zipwriter;1", "nsIZipWriter");
var zipW = new zipWriter();

zipW.open(file, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
zipW.addEntryFile(file11.path, Components.interfaces.nsIZipWriter.COMPRESSION_DEFAULT, file11, false);
zipW.close();

使用上面的代码压缩文件不会创建......请帮帮我......这段代码有什么问题......还有其他方法可以在javascript的帮助下创建一个zip文件夹......提前致谢

4

1 回答 1

0

我得到了上述问题的解决方案......

    var file = Components.classes["@mozilla.org/file/directory_service;1"].
           getService(Components.interfaces.nsIProperties).
           get("AChrom", Components.interfaces.nsIFile);

           file.initWithPath(file.path+"\\smad.zip");


     var filenew = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("AChrom", Components.interfaces.nsIFile);

           filenew.initWithPath(filenew.path+"\\home.txt"); 


    const PR_RDONLY      = 0x01;
    const PR_WRONLY      = 0x02;
    const PR_RDWR        = 0x04;
    const PR_CREATE_FILE = 0x08;
    const PR_APPEND      = 0x10;
    const PR_TRUNCATE    = 0x20;
    const PR_SYNC        = 0x40;
   const PR_EXCL        = 0x80;
    var zipWriter = Components.Constructor("@mozilla.org/zipwriter;1", "nsIZipWriter");
    var zipW = new zipWriter();

zipW.open(file, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
zipW.addEntryFile("home.txt", Components.interfaces.nsIZipWriter.COMPRESSION_DEFAULT, filenew, false);
zipW.close();
于 2013-08-23T07:18:55.337 回答