XUL : Unzip a File with nsIZipReader

I got some difficulties to do that. So this is my script.

You need to give a nsIFile to the zip file and the path where it should be unzip. Label and progressBar are Optionals, I but them because it was easier for my project (WeMov).

I’m a beginner in JavaScript so please leave a comment if you have any comments.

Documentation of nsIZipReader

this.unZip = function (ZipFile,Path,Label,totalProgressBar,statusProgressbar) 
{
	var zipReader = Components	.classes["@mozilla.org/libjar/zip-reader;1"]
								.createInstance(Components.interfaces.nsIZipReader);
	zipReader.open(ZipFile);
	zipReader.test(null);//test integrity of all file in the zip
	entries = zipReader.findEntries(null);
 
	//-- INFO -------------------------------------------------------------------------------------
	var NbFiles = NbDir=Counter=0;
	while (entries.hasMore()) 
	{
		var Name = entries.getNext();
		if(Name.charAt(Name.length-1) == '/')
		NbDir++;
		else
		NbFiles++;
	}
	var StringLabel = document.getElementById("sbsettings");
	if(totalProgressBar != null)
		totalProgressBar.value = 55;
	//-- INFO -------------------------------------------------------------------------------------
 
	// create directories first
	if(Label != null)
		Label.value = StringLabel.getString("progressBarCreateDirectories")+" 0/"+NbDir;
	var entries = zipReader.findEntries("*/");//get all directories
	while (entries.hasMore()) 
	{
		//-- INFO -------------------------------------------------------------------------------------
		Counter++;
		if(Label != null)
			Label.value = StringLabel.getString("progressBarCreateDirectories")+" "+Counter+"/"+NbDir;
		if(statusProgressbar != null)
			statusProgressbar.value = Counter/NbDir*100;
		//-- INFO -------------------------------------------------------------------------------------
		var SubPath = Path + this.CleanPath(entries.getNext());
 
		var target = Components	.classes["@mozilla.org/file/local;1"]
								.createInstance(Components.interfaces.nsILocalFile);
		target.initWithPath(SubPath);
 
		if (target.exists()) //directory exist -> try next one
			continue;
		else
			target.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0777);
	}
	Counter=0;
	if(totalProgressBar != null)
		totalProgressBar.value = 70;
	//UNZIP FILES
	entries = zipReader.findEntries(null);//get every things in the zip file
	while (entries.hasMore()) 
	{
		//-- INFO -------------------------------------------------------------------------------------
		Counter++;
		if(Label != null)
			Label.value = StringLabel.getString("progressBarUnzip")+" "+Counter+"/"+(NbFiles+NbDir);
		if(statusProgressbar != null)
			statusProgressbar.value = Counter/(NbDir+NbFiles)*100;
		//-- INFO -------------------------------------------------------------------------------------
 
		var NameFile = entries.getNext();
		var SubPath = Path + this.CleanPath(NameFile);
 
		var target = Components	.classes["@mozilla.org/file/local;1"]
								.createInstance(Components.interfaces.nsILocalFile);
		target.initWithPath(SubPath);
		if (target.exists())
			continue;
		else
			zipReader.extract(NameFile, target);
	}
	zipReader.close();
	ZipFile.remove(true);
	return true;//*/
}
This entry was posted in Code Snippet, XUL and tagged , . Bookmark the permalink.

2 Responses to XUL : Unzip a File with nsIZipReader

  1. da best. Keep it going! Thank you

  2. Thomas says:

    Thanks !

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">