Пример #1
0
  public static String CreateZip(String[] filesToZip, String zipFileName) {

    byte[] buffer = new byte[18024];

    try {

      ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
      out.setLevel(Deflater.BEST_COMPRESSION);
      for (int i = 0; i < filesToZip.length; i++) {
        FileInputStream in = new FileInputStream(filesToZip[i]);
        String fileName = null;
        for (int X = filesToZip[i].length() - 1; X >= 0; X--) {
          if (filesToZip[i].charAt(X) == '\\' || filesToZip[i].charAt(X) == '/') {
            fileName = filesToZip[i].substring(X + 1);
            break;
          } else if (X == 0) fileName = filesToZip[i];
        }
        out.putNextEntry(new ZipEntry(fileName));
        int len;
        while ((len = in.read(buffer)) > 0) out.write(buffer, 0, len);
        out.closeEntry();
        in.close();
      }
      out.close();
    } catch (IllegalArgumentException e) {
      return "Failed to create zip: " + e.toString();
    } catch (FileNotFoundException e) {
      return "Failed to create zip: " + e.toString();
    } catch (IOException e) {
      return "Failed to create zip: " + e.toString();
    }
    return "Success";
  }
  /** initializes internal hash tables with Jar file resources. */
  private byte[] read(String name) {
    try {
      // extracts just sizes only.
      ZipFile zf = new ZipFile(jarFileName);
      Enumeration e = zf.entries();
      while (e.hasMoreElements()) {
        ZipEntry ze = (ZipEntry) e.nextElement();
        if (debugOn) {
          System.out.println(dumpZipEntry(ze));
        }
        htSizes.put(ze.getName(), new Integer((int) ze.getSize()));
      }
      zf.close();

      // extract resources and put them into the hashtable.
      FileInputStream fis = new FileInputStream(jarFileName);
      BufferedInputStream bis = new BufferedInputStream(fis);
      ZipInputStream zis = new ZipInputStream(bis);
      ZipEntry ze = null;
      while ((ze = zis.getNextEntry()) != null) {
        if (ze.isDirectory()) {
          continue;
        }
        if (debugOn) {
          System.out.println("ze.getName()=" + ze.getName() + "," + "getSize()=" + ze.getSize());
        }
        int size = (int) ze.getSize();
        // -1 means unknown size.
        if (size == -1) {
          size = ((Integer) htSizes.get(ze.getName())).intValue();
        }
        byte[] b = new byte[(int) size];
        int rb = 0;
        int chunk = 0;
        while (((int) size - rb) > 0) {
          chunk = zis.read(b, rb, (int) size - rb);
          if (chunk == -1) {
            break;
          }
          rb += chunk;
        }

        if (debugOn) {
          System.out.println(
              ze.getName() + "  rb=" + rb + ",size=" + size + ",csize=" + ze.getCompressedSize());
        }

        if (ze.getName().equals(name)) {
          return b;
        }
      }
    } catch (NullPointerException e) {
      System.out.println("done.");
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    return null;
  }
Пример #3
0
// =============================================================================
Пример #4
0
  // {{{ run() method
  public void run() {
    /* if the VFS supports renaming files, we first
     * save to #<filename>#save#, then rename that
     * to <filename>, so that if the save fails,
     * data will not be lost.
     *
     * as of 4.1pre7 we now call vfs.getTwoStageSaveName()
     * instead of constructing the path directly
     * since some VFS's might not allow # in filenames.
     */

    boolean vfsRenameCap = (vfs.getCapabilities() & VFS.RENAME_CAP) != 0;

    boolean wantTwoStage = wantTwoStageSave(buffer);
    boolean twoStageSave = vfsRenameCap && wantTwoStage;

    try {
      String[] args = {vfs.getFileName(path)};
      setStatus(jEdit.getProperty("vfs.status.save", args));

      // the entire save operation can be aborted...
      setAbortable(true);

      path = vfs._canonPath(session, path, view);
      if (!MiscUtilities.isURL(path)) path = MiscUtilities.resolveSymlinks(path);

      String savePath;
      if (twoStageSave) {
        savePath = vfs.getTwoStageSaveName(path);
        if (savePath == null) {
          throw new IOException("Can't get a temporary path for two-stage save: " + path);
        }
      } else {
        makeBackup();
        savePath = path;
      }

      OutputStream out = vfs._createOutputStream(session, savePath, view);
      if (out == null) {
        buffer.setBooleanProperty(ERROR_OCCURRED, true);
        return;
      }
      try {
        // this must be after the stream is created or
        // we deadlock with SSHTools.
        buffer.readLock();
        try {
          // Can't use buffer.getName() here because
          // it is not changed until the save is
          // complete
          if (path.endsWith(".gz")) buffer.setBooleanProperty(Buffer.GZIPPED, true);
          else if (buffer.getName().endsWith(".gz")) {
            // The path do not ends with gz.
            // The buffer name was .gz.
            // So it means it's blabla.txt.gz -> blabla.txt, I remove
            // the gz property
            buffer.setBooleanProperty(Buffer.GZIPPED, false);
          }

          if (buffer.getBooleanProperty(Buffer.GZIPPED)) out = new GZIPOutputStream(out);

          write(buffer, out);
        } finally {
          buffer.readUnlock();
        }
      } finally {
        IOUtilities.closeQuietly(out);
      }

      if (twoStageSave) {
        makeBackup();
        if (!vfs._rename(session, savePath, path, view))
          throw new IOException("Rename failed: " + savePath);
      }

      if (!twoStageSave) VFSManager.sendVFSUpdate(vfs, path, true);
    } catch (FileNotFoundException e) {
      Log.log(Log.ERROR, this, "Unable to save buffer " + e);
      String[] pp = {e.getMessage()};
      VFSManager.error(view, path, "ioerror.write-error", pp);

      buffer.setBooleanProperty(ERROR_OCCURRED, true);
    } catch (UnsupportedCharsetException e) {
      Log.log(Log.ERROR, this, e, e);
      String[] pp = {e.getCharsetName()};
      VFSManager.error(view, path, "ioerror.unsupported-encoding-error", pp);

      buffer.setBooleanProperty(ERROR_OCCURRED, true);
    } catch (Exception e) {
      Log.log(Log.ERROR, this, e);
      String[] pp = {e.toString()};
      VFSManager.error(view, path, "ioerror.write-error", pp);

      buffer.setBooleanProperty(ERROR_OCCURRED, true);
    } catch (WorkThread.Abort a) {
      buffer.setBooleanProperty(ERROR_OCCURRED, true);
    } finally {
      try {
        vfs._saveComplete(session, buffer, path, view);
        if (twoStageSave) {
          vfs._finishTwoStageSave(session, buffer, path, view);
        }
        // clean up left-over markers file
        if (!jEdit.getBooleanProperty("persistentMarkers"))
          vfs._delete(session, Buffer.getMarkersPath(vfs, path), view);
        vfs._endVFSSession(session, view);
      } catch (Exception e) {
        Log.log(Log.ERROR, this, e);
        String[] pp = {e.toString()};
        VFSManager.error(view, path, "ioerror.write-error", pp);

        buffer.setBooleanProperty(ERROR_OCCURRED, true);
      } catch (WorkThread.Abort a) {
        buffer.setBooleanProperty(ERROR_OCCURRED, true);
      }
    }
  } // }}}
Пример #5
0
 /** Parses command line arguments. */
 boolean parseArgs(String args[]) {
   /* Preprocess and expand @file arguments */
   try {
     args = CommandLine.parse(args);
   } catch (FileNotFoundException e) {
     fatalError(formatMsg("error.cant.open", e.getMessage()));
     return false;
   } catch (IOException e) {
     fatalError(e);
     return false;
   }
   /* parse flags */
   int count = 1;
   try {
     String flags = args[0];
     if (flags.startsWith("-")) {
       flags = flags.substring(1);
     }
     for (int i = 0; i < flags.length(); i++) {
       switch (flags.charAt(i)) {
         case 'c':
           if (xflag || tflag || uflag || iflag) {
             usageError();
             return false;
           }
           cflag = true;
           break;
         case 'u':
           if (cflag || xflag || tflag || iflag) {
             usageError();
             return false;
           }
           uflag = true;
           break;
         case 'x':
           if (cflag || uflag || tflag || iflag) {
             usageError();
             return false;
           }
           xflag = true;
           break;
         case 't':
           if (cflag || uflag || xflag || iflag) {
             usageError();
             return false;
           }
           tflag = true;
           break;
         case 'M':
           Mflag = true;
           break;
         case 'v':
           vflag = true;
           break;
         case 'f':
           fname = args[count++];
           break;
         case 'm':
           mname = args[count++];
           break;
         case '0':
           flag0 = true;
           break;
         case 'i':
           if (cflag || uflag || xflag || tflag) {
             usageError();
             return false;
           }
           // do not increase the counter, files will contain rootjar
           rootjar = args[count++];
           iflag = true;
           break;
         case 'e':
           ename = args[count++];
           break;
         case 'p':
           pname = args[count++];
           break;
         default:
           error(formatMsg("error.illegal.option", String.valueOf(flags.charAt(i))));
           usageError();
           return false;
       }
     }
   } catch (ArrayIndexOutOfBoundsException e) {
     usageError();
     return false;
   }
   if (!cflag && !tflag && !xflag && !uflag && !iflag) {
     error(getMsg("error.bad.option"));
     usageError();
     return false;
   }
   /* parse file arguments */
   int n = args.length - count;
   if (n > 0) {
     int k = 0;
     String[] nameBuf = new String[n];
     try {
       for (int i = count; i < args.length; i++) {
         if (args[i].equals("-C")) {
           /* change the directory */
           String dir = args[++i];
           dir = (dir.endsWith(File.separator) ? dir : (dir + File.separator));
           dir = dir.replace(File.separatorChar, '/');
           while (dir.indexOf("//") > -1) {
             dir = dir.replace("//", "/");
           }
           paths.add(dir.replace(File.separatorChar, '/'));
           nameBuf[k++] = dir + args[++i];
         } else {
           nameBuf[k++] = args[i];
         }
       }
     } catch (ArrayIndexOutOfBoundsException e) {
       usageError();
       return false;
     }
     files = new String[k];
     System.arraycopy(nameBuf, 0, files, 0, k);
   } else if (cflag && (mname == null)) {
     error(getMsg("error.bad.cflag"));
     usageError();
     return false;
   } else if (uflag) {
     if ((mname != null) || (ename != null) || (pname != null)) {
       /* just want to update the manifest */
       return true;
     } else {
       error(getMsg("error.bad.uflag"));
       usageError();
       return false;
     }
   }
   return true;
 }