Exemplo n.º 1
0
  /** Adds a new file entry to the ZIP output stream. */
  void addFile(ZipOutputStream zos, File file) throws IOException {
    String name = file.getPath();
    boolean isDir = file.isDirectory();
    if (isDir) {
      name = name.endsWith(File.separator) ? name : (name + File.separator);
    }
    name = entryName(name);

    if (name.equals("") || name.equals(".") || name.equals(zname)) {
      return;
    } else if ((name.equals(MANIFEST_DIR) || name.equals(MANIFEST_NAME)) && !Mflag) {
      if (vflag) {
        output(formatMsg("out.ignore.entry", name));
      }
      return;
    }

    long size = isDir ? 0 : file.length();

    if (vflag) {
      out.print(formatMsg("out.adding", name));
    }
    ZipEntry e = new ZipEntry(name);
    e.setTime(file.lastModified());
    if (size == 0) {
      e.setMethod(ZipEntry.STORED);
      e.setSize(0);
      e.setCrc(0);
    } else if (flag0) {
      crc32File(e, file);
    }
    zos.putNextEntry(e);
    if (!isDir) {
      copy(file, zos);
    }
    zos.closeEntry();
    /* report how much compression occurred. */
    if (vflag) {
      size = e.getSize();
      long csize = e.getCompressedSize();
      out.print(formatMsg2("out.size", String.valueOf(size), String.valueOf(csize)));
      if (e.getMethod() == ZipEntry.DEFLATED) {
        long ratio = 0;
        if (size != 0) {
          ratio = ((size - csize) * 100) / size;
        }
        output(formatMsg("out.deflated", String.valueOf(ratio)));
      } else {
        output(getMsg("out.stored"));
      }
    }
  }
Exemplo n.º 2
0
 static String getResourceAsString(String path) {
   InputStream is = null;
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   PrintStream outPrint = new PrintStream(out);
   try {
     is = Util.class.getClassLoader().getResourceAsStream(path);
     int content;
     while ((content = is.read()) != -1) {
       // convert to char and display it
       outPrint.print((char) content);
     }
   } catch (IOException e) {
     e.printStackTrace();
   } finally {
     try {
       if (is != null) is.close();
       if (outPrint != null) outPrint.close();
     } catch (IOException ex) {
       ex.printStackTrace();
     }
   }
   return out.toString();
 }
Exemplo n.º 3
0
 /**
  * automatically generate non-existing help html files for existing commands in the CommandLoader
  *
  * @param cl the CommandLoader where you look for the existing commands
  */
 public void createHelpText(CommandLoader cl) {
   File dir = new File((getClass().getResource("..").getPath()));
   dir = new File(dir, language);
   if (!dir.exists() && !dir.mkdirs()) {
     System.out.println("Failed to create " + dir.getAbsolutePath());
     return;
   }
   for (String mnemo : cl.getMnemoList()) {
     if (!exists(mnemo)) {
       File file = new File(dir, mnemo + ".htm");
       try {
         file.createNewFile();
         BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(file));
         PrintStream ps = new PrintStream(os);
         ps.println("<html>\n<head>\n<title>" + mnemo + "\n</title>\n</head>\n<body>");
         ps.println("Command: " + mnemo.toUpperCase() + "<br>");
         ps.println("arguments: <br>");
         ps.println("effects: <br>");
         ps.println("flags to be set: <br>");
         ps.println("approx. clockcycles: <br>");
         ps.println("misc: <br>");
         ps.println("</body>\n</html>");
         ps.flush();
         ps.close();
       } catch (IOException e) {
         System.out.println("failed to create " + file.getAbsolutePath());
       }
     }
   }
 }
Exemplo n.º 4
0
  /** Starts main program with the specified arguments. */
  public synchronized boolean run(String args[]) {
    ok = true;
    if (!parseArgs(args)) {
      return false;
    }
    try {
      if (cflag || uflag) {
        if (fname != null) {
          // The name of the zip file as it would appear as its own
          // zip file entry. We use this to make sure that we don't
          // add the zip file to itself.
          zname = fname.replace(File.separatorChar, '/');
          if (zname.startsWith("./")) {
            zname = zname.substring(2);
          }
        }
      }
      if (cflag) {
        Manifest manifest = null;
        InputStream in = null;

        if (!Mflag) {
          if (mname != null) {
            in = new FileInputStream(mname);
            manifest = new Manifest(new BufferedInputStream(in));
          } else {
            manifest = new Manifest();
          }
          addVersion(manifest);
          addCreatedBy(manifest);
          if (isAmbiguousMainClass(manifest)) {
            if (in != null) {
              in.close();
            }
            return false;
          }
          if (ename != null) {
            addMainClass(manifest, ename);
          }
          if (pname != null) {
            if (!addProfileName(manifest, pname)) {
              if (in != null) {
                in.close();
              }
              return false;
            }
          }
        }
        OutputStream out;
        if (fname != null) {
          out = new FileOutputStream(fname);
        } else {
          out = new FileOutputStream(FileDescriptor.out);
          if (vflag) {
            // Disable verbose output so that it does not appear
            // on stdout along with file data
            // error("Warning: -v option ignored");
            vflag = false;
          }
        }
        expand(null, files, false);
        create(new BufferedOutputStream(out, 4096), manifest);
        if (in != null) {
          in.close();
        }
        out.close();
      } else if (uflag) {
        File inputFile = null, tmpFile = null;
        FileInputStream in;
        FileOutputStream out;
        if (fname != null) {
          inputFile = new File(fname);
          tmpFile = createTempFileInSameDirectoryAs(inputFile);
          in = new FileInputStream(inputFile);
          out = new FileOutputStream(tmpFile);
        } else {
          in = new FileInputStream(FileDescriptor.in);
          out = new FileOutputStream(FileDescriptor.out);
          vflag = false;
        }
        InputStream manifest = (!Mflag && (mname != null)) ? (new FileInputStream(mname)) : null;
        expand(null, files, true);
        boolean updateOk = update(in, new BufferedOutputStream(out), manifest, null);
        if (ok) {
          ok = updateOk;
        }
        in.close();
        out.close();
        if (manifest != null) {
          manifest.close();
        }
        if (ok && fname != null) {
          // on Win32, we need this delete
          inputFile.delete();
          if (!tmpFile.renameTo(inputFile)) {
            tmpFile.delete();
            throw new IOException(getMsg("error.write.file"));
          }
          tmpFile.delete();
        }
      } else if (tflag) {
        replaceFSC(files);
        if (fname != null) {
          list(fname, files);
        } else {
          InputStream in = new FileInputStream(FileDescriptor.in);
          try {
            list(new BufferedInputStream(in), files);
          } finally {
            in.close();
          }
        }
      } else if (xflag) {
        replaceFSC(files);
        if (fname != null && files != null) {
          extract(fname, files);
        } else {
          InputStream in =
              (fname == null) ? new FileInputStream(FileDescriptor.in) : new FileInputStream(fname);
          try {
            extract(new BufferedInputStream(in), files);
          } finally {
            in.close();
          }
        }
      } else if (iflag) {
        genIndex(rootjar, files);
      }
    } catch (IOException e) {
      fatalError(e);
      ok = false;
    } catch (Error ee) {
      ee.printStackTrace();
      ok = false;
    } catch (Throwable t) {
      t.printStackTrace();
      ok = false;
    }
    out.flush();
    err.flush();
    return ok;
  }
Exemplo n.º 5
0
 /** Print an error mesage; like something is broken */
 protected void error(String s) {
   err.println(s);
 }
Exemplo n.º 6
0
 /** Print an output message; like verbose output and the like */
 protected void output(String s) {
   out.println(s);
 }