Any way to read zip files (and unzip the content) in PJSR

Juan Conejero

PixInsight Staff
Sep 2, 2004
8,719
714
58
Valencia, Spain
pixinsight.com
Hi Jean-Marc,

There is no direct way to use ZIP files in PJSR. However, you can execute an external process, such as the unzip utility program, from a JavaScript script using the ExternalProcess core object.

Consider the following utility function:

JavaScript:
function run( program, args )
{
   let P = new ExternalProcess( program, args );
   if ( P.waitForStarted() )
   {
      processEvents();
      let n = 0;
      for ( ; n < 10 && !P.waitForFinished( 250 ); ++n )
      {
         console.write( "<end>\b" + "-/|\\".charAt( n%4 ) );
         processEvents();
      }
      if ( n > 0 )
         console.writeln( "<end>\b" );
   }
   if ( P.exitStatus == ProcessExitStatus_Crash || P.exitCode != 0 )
   {
      let e = P.stderr;
      throw new Error( "Process failed: " + program + ((e.length > 0) ? "\n" + e : "") );
   }
}
Now you can call it as follows to unzip the contents of a zip file into a prescribed directory:

run( "unzip", ["/path/to/foo.zip", "-d", "/path/to/foo-dir"] );

Once unzipped, the contents can be explored with the following global method:

Array searchDirectory( String dirPath[, Boolean recursive=false] )

and accessed using methods of the File object.

Let me know if this helps.
 

bitli

PTeam Member
Feb 1, 2009
545
11
Switzerland
Finally used (on Windows 10), it execute the unzip successfully, but if I relaunch the script I get:

Downloading 'https://SOMEURL.zip' to 'c:/temp/myfile.zip'

*** Error: File I/O Error: Unable to create file: Win32 error (32): The process cannot access the

: c:/temp/myfile.zipfile because it is being used by another process.

*** Error: Failure writing output to destination

It seems the process is not fully terminated. I tried to add an explicit P.terminate(), but this did no change anything.

Finally it seems to work if I explicitly close the streams as follow:
JavaScript:
function run( program, args )
{
   let P = new ExternalProcess( program, args );
   if ( P.waitForStarted() )
   {
      processEvents();
      let n = 0;
      for ( ; n < 10 && !P.waitForFinished( 250 ); ++n )
      {
         console.write( "<end>\b" + "-/|\\".charAt( n%4 ) );
         processEvents();
      }
      if ( n > 0 )
         console.writeln( "<end>\b" );
   }

   if ( P.exitStatus == ProcessExitStatus_Crash || P.exitCode != 0 )
   {
      let e = P.stderr;
      throw new Error( "Process failed: " + program + ((e.length > 0) ? "\n" + e : "") );
   }

   P.terminate();
   P.closeStandardError();
   P.closeStandardInput();
   P.closeStandardOutput();

}
 
Last edited: