Logo Search packages:      
Sourcecode: jlha-utils version File versions  Download package

void org::jlhafrontend::JLHAFrontEnd::extract ( Opts  useropts,
String  archiveFilename,
String[]  filesFilename 
) [inline]

execute extract command

Parameters:
useropts Options input by users
archiveFilename the filename of the archive file
filesFilename the array of filenames to be extracted

Definition at line 785 of file JLHAFrontEnd.java.

References addFileSeparator(), checkDirTraversal(), org::jlhafrontend::Opts::getEUCMode(), org::jlhafrontend::Opts::getForce(), org::jlhafrontend::Opts::getIgnorePath(), org::jlhafrontend::Opts::getQuiet(), org::jlhafrontend::Opts::getSpecifyExtractDirectory(), org::jlhafrontend::Opts::getTextMode(), org::jlhafrontend::Opts::getVerbose(), isInArray(), isProbableSymLink(), and mylogger.

Referenced by start().

                                                                                       {
      LhaFile archiveFile=null;
      LhaHeader[] lhaEntries=null;
      int i,j,k=0;
      InputStream compressedInputStream=null;
      OutputStream uncompressedOutputStream=null;
      boolean force=false,ignorePath=false,verbose=false;
      File uncompressedFile,uncompressedFileParent=null;
      String filenameOrigin=null,workingDir=null,filename=null;
      int quiet=0;
      byte[] buf = new byte[1024*8];
      int bufRead=0;

      /* open archive file */
      try {
          archiveFile = new LhaFile (new File(archiveFilename));
      } catch (FileNotFoundException e) {
          System.err.println("LHa: Fatal error: "+archiveFilename+": No such file or directory");
          return;
      } catch (Exception e) {
          mylogger.warning("Open "+archiveFilename+" error:"+e.toString());             System.err.println("LHa: Warning: Checksum error (LHarc file?)");
          System.err.println("LHa: Fatal error: Unkonwn header (lha file?)");
          return;
      }
      
      /* get opts */
      force = useropts.getForce();
      ignorePath = useropts.getIgnorePath();
      verbose = useropts.getVerbose();
      workingDir = useropts.getSpecifyExtractDirectory();
      quiet=useropts.getQuiet();

      /* list archive file */
      lhaEntries = archiveFile.getEntries();

      /* extract entries */
      for (i=0 ; i<lhaEntries.length; i++) {
          filenameOrigin = lhaEntries[i].getPath();
          /* remove beginning '/'s */
          while (filenameOrigin.startsWith(File.separator)) {
              filenameOrigin = filenameOrigin.substring(File.separator.length());
          }
          /* check if the file is in list or not */
          if (filesFilename != null && !isInArray(lhaEntries[i].getPath(),filesFilename)) {
            continue;
          }
          /* check dir traversal for security */
            if (!ignorePath && checkDirTraversal(filenameOrigin)) {
                System.err.println("Arbitrary directory traversal attempt: "+filenameOrigin);
               continue;
            }
            /* generate filename */
            if (ignorePath) {
                File tmpfile=null;
                tmpfile = new File(filenameOrigin);
                filename = tmpfile.getName();
                tmpfile = null;
            } else {
                filename = filenameOrigin;
            }
          /* add working directory */
          if (workingDir != null) {
              File tmpfile=null;
            filename = addFileSeparator(workingDir)+filename;
            tmpfile = new File(workingDir);
            if (tmpfile != null) {
              tmpfile.mkdirs();
                }
                tmpfile=null;
          }
            /* create uncompressedFile */
          uncompressedFile = new File(filename);
          /* extracting directories */
          if (!ignorePath 
          && lhaEntries[i].getCompressMethod().compareTo(CompressMethod.LHD)==0) {
              if (lhaEntries[i].getOSID()!='U' || !isProbableSymLink(filename)) {  /* FIXME - should not handle symlink like this */
                      uncompressedFile.mkdirs();
                }
            if (verbose) {
                System.out.println("Making directory \""+filename+"\".");
            }
            continue;
          }
          if (!force && uncompressedFile.exists()) {
              if (quiet == 0) {
              System.out.println(filename+" : Skipped...");
                }
            continue;
          }
          if (!ignorePath) {
            uncompressedFileParent = uncompressedFile.getParentFile();
            if (uncompressedFileParent != null) {
                uncompressedFileParent.mkdirs();
                }
          }
          if (quiet == 0) {
              System.out.print(filename);
            }
            k = filename.length();
          if (quiet == 0) {
            System.out.print("\t- Melted   :  ");
            }
          System.out.flush();
          compressedInputStream = archiveFile.getInputStream(lhaEntries[i]);
          try {
            uncompressedOutputStream = new FileOutputStream(uncompressedFile);
          } catch (Exception e) {
            mylogger.severe("open OutputStream error "+e.toString());
            continue;
          }
          if (useropts.getTextMode()) {
            uncompressedOutputStream = new DOS2UnixOutputStream(uncompressedOutputStream);
          }
          if (useropts.getEUCMode()) {
            uncompressedOutputStream = new SJIS2EUCJPOutputStream(uncompressedOutputStream);
          }

          for (j=0; true; j++) {
            try {
                bufRead=compressedInputStream.read(buf);
            } catch (Exception e) {
                mylogger.severe("read from compressedInputStream "+e.toString());
                break;
            }
            if (bufRead<=0) break;
            if (j<80 - ((k/8)+1)*8 - 14 - 1) {
                if (quiet == 0) {
                  System.out.print("o");
                    }
                System.out.flush();
            }
            try {
                uncompressedOutputStream.write(buf,0,bufRead);
            } catch (Exception e) {
                mylogger.severe("write to uncompressedOutputStream "+e.toString());
                break;
            }
          }

          try {
            compressedInputStream.close();
          } catch (IOException e) {
            mylogger.severe("closing compressedInputStream "+e.toString());
          }
          try {
            uncompressedOutputStream.close();
          } catch (IOException e) {
            mylogger.severe("closing uncompressedOutputStream "+e.toString());
          }
            if (quiet == 0) {
            System.out.println("");
            }
      }

      try {
          archiveFile.close();
      } catch (IOException e) {
          mylogger.warning("Close "+archiveFilename+" error:"+e.toString());
      }
      
    }


Generated by  Doxygen 1.6.0   Back to index