Example usage for java.util.zip ZipEntry setComment

List of usage examples for java.util.zip ZipEntry setComment

Introduction

In this page you can find the example usage for java.util.zip ZipEntry setComment.

Prototype

public void setComment(String comment) 

Source Link

Document

Sets the optional comment string for the entry.

Usage

From source file:org.signserver.anttasks.PostProcessModulesTask.java

/**
 * Replacer for the postprocess-jar Ant macro.
 * /*  w  ww  . ja  v a2s . com*/
 * @param replaceincludes Ant list of all files in the jar to replace in
 * @param src Source jar file
 * @param destfile Destination jar file
 * @param properties Properties to replace from
 * @param self The Task (used for logging)
 * @throws IOException in case of error
 */
protected void replaceInJar(String replaceincludes, String src, String destfile, Map properties, Task self)
        throws IOException {
    try {
        self.log("Replace " + replaceincludes + " in " + src + " to " + destfile, Project.MSG_VERBOSE);

        File srcFile = new File(src);
        if (!srcFile.exists()) {
            throw new FileNotFoundException(srcFile.getAbsolutePath());
        }

        // Expand properties of all files in replaceIncludes
        HashSet<String> replaceFiles = new HashSet<String>();
        String[] rfiles = replaceincludes.split(",");
        for (int i = 0; i < rfiles.length; i++) {
            rfiles[i] = rfiles[i].trim();
        }
        replaceFiles.addAll(Arrays.asList(rfiles));
        self.log("Files to replace: " + replaceFiles, Project.MSG_INFO);

        // Open source zip file
        ZipFile zipSrc = new ZipFile(srcFile);
        ZipOutputStream zipDest = new ZipOutputStream(new FileOutputStream(destfile));

        // For each entry in the source file copy them to dest file and postprocess if necessary
        Enumeration<? extends ZipEntry> entries = zipSrc.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            String name = entry.getName();

            if (entry.isDirectory()) {
                // Just put the directory
                zipDest.putNextEntry(entry);
            } else {
                // If we should postprocess the entry
                if (replaceFiles.contains(name)) {
                    name += (" [REPLACE]");
                    self.log(name, Project.MSG_VERBOSE);

                    // Create a new zip entry for the file
                    ZipEntry newEntry = new ZipEntry(entry.getName());
                    newEntry.setComment(entry.getComment());
                    newEntry.setExtra(entry.getExtra());
                    zipDest.putNextEntry(newEntry);

                    // Read the old document
                    StringBuffer oldDocument = stringBufferFromFile(zipSrc.getInputStream(entry));
                    self.log("Before replace ********\n" + oldDocument.toString() + "\n", Project.MSG_DEBUG);

                    // Do properties substitution
                    StrSubstitutor sub = new StrSubstitutor(properties);
                    StringBuffer newerDocument = commentReplacement(oldDocument, properties);
                    String newDocument = sub.replace(newerDocument);
                    self.log("After replace ********\n" + newDocument.toString() + "\n", Project.MSG_DEBUG);

                    // Write the new document
                    byte[] newBytes = newDocument.getBytes("UTF-8");
                    entry.setSize(newBytes.length);
                    copy(new ByteArrayInputStream(newBytes), zipDest);
                } else {
                    // Just copy the entry to dest zip file
                    name += (" []");
                    self.log(name, Project.MSG_VERBOSE);
                    zipDest.putNextEntry(entry);
                    copy(zipSrc.getInputStream(entry), zipDest);
                }
                zipDest.closeEntry();
            }
        }
        zipSrc.close();
        zipDest.close();
    } catch (IOException ex) {
        throw new BuildException(ex);
    }
}

From source file:plugin.games.data.trans.step.fileoutput.TextFileOutput.java

public void openNewFile(String baseFilename) throws KettleException {
    if (baseFilename == null) {
        throw new KettleFileException(BaseMessages.getString(PKG, "TextFileOutput.Exception.FileNameNotSet")); //$NON-NLS-1$
    }//w w  w  . ja  va  2  s .co  m

    data.writer = null;

    ResultFile resultFile = null;

    String filename = buildFilename(environmentSubstitute(baseFilename), true);

    try {
        if (meta.isServletOutput()) {
            Writer writer = getTrans().getServletPrintWriter();
            if (Const.isEmpty(meta.getEncoding())) {
                data.writer = new WriterOutputStream(writer);
            } else {
                data.writer = new WriterOutputStream(writer, meta.getEncoding());
            }

        } else if (meta.isFileAsCommand()) {
            if (log.isDebug())
                logDebug("Spawning external process");
            if (data.cmdProc != null) {
                logError("Previous command not correctly terminated");
                setErrors(1);
            }
            String cmdstr = environmentSubstitute(meta.getFileName());
            if (Const.getOS().equals("Windows 95")) {
                cmdstr = "command.com /C " + cmdstr;
            } else {
                if (Const.getOS().startsWith("Windows")) {
                    cmdstr = "cmd.exe /C " + cmdstr;
                }
            }
            if (isDetailed())
                logDetailed("Starting: " + cmdstr);
            Runtime r = Runtime.getRuntime();
            data.cmdProc = r.exec(cmdstr, EnvUtil.getEnvironmentVariablesForRuntimeExec());
            data.writer = data.cmdProc.getOutputStream();
            StreamLogger stdoutLogger = new StreamLogger(log, data.cmdProc.getInputStream(), "(stdout)");
            StreamLogger stderrLogger = new StreamLogger(log, data.cmdProc.getErrorStream(), "(stderr)");
            new Thread(stdoutLogger).start();
            new Thread(stderrLogger).start();
        } else {

            // Check for parent folder
            createParentFolder(filename);

            // Add this to the result file names...
            resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL,
                    KettleVFS.getFileObject(filename, getTransMeta()), getTransMeta().getName(), getStepname());
            resultFile.setComment("This file was created with a text file output step");
            addResultFile(resultFile);

            OutputStream outputStream;

            if (!Const.isEmpty(meta.getFileCompression())
                    && !meta.getFileCompression().equals(FILE_COMPRESSION_TYPE_NONE)) {
                if (meta.getFileCompression().equals(FILE_COMPRESSION_TYPE_ZIP)) {
                    if (log.isDetailed())
                        logDetailed("Opening output stream in zipped mode");

                    if (checkPreviouslyOpened(filename)) {
                        data.fos = KettleVFS.getOutputStream(filename, getTransMeta(), true);
                    } else {
                        data.fos = KettleVFS.getOutputStream(filename, getTransMeta(), meta.isFileAppended());
                    }
                    data.zip = new ZipOutputStream(data.fos);
                    File entry = new File(filename);
                    ZipEntry zipentry = new ZipEntry(entry.getName());
                    zipentry.setComment("Compressed by Kettle");
                    data.zip.putNextEntry(zipentry);
                    outputStream = data.zip;
                } else if (meta.getFileCompression().equals(FILE_COMPRESSION_TYPE_GZIP)) {
                    if (log.isDetailed())
                        logDetailed("Opening output stream in gzipped mode");
                    if (checkPreviouslyOpened(filename)) {
                        data.fos = KettleVFS.getOutputStream(filename, getTransMeta(), true);
                    } else {
                        data.fos = KettleVFS.getOutputStream(filename, getTransMeta(), meta.isFileAppended());
                    }
                    data.gzip = new GZIPOutputStream(data.fos);
                    outputStream = data.gzip;
                } else {
                    throw new KettleFileException("No compression method specified!");
                }
            } else {
                if (log.isDetailed())
                    logDetailed("Opening output stream in nocompress mode");
                if (checkPreviouslyOpened(filename)) {
                    data.fos = KettleVFS.getOutputStream(filename, getTransMeta(), true);
                } else {
                    data.fos = KettleVFS.getOutputStream(filename, getTransMeta(), meta.isFileAppended());
                }
                outputStream = data.fos;
            }

            if (!Const.isEmpty(meta.getEncoding())) {
                if (log.isDetailed())
                    logDetailed("Opening output stream in encoding: " + meta.getEncoding());
                data.writer = new BufferedOutputStream(outputStream, 5000);
            } else {
                if (log.isDetailed())
                    logDetailed("Opening output stream in default encoding");
                data.writer = new BufferedOutputStream(outputStream, 5000);
            }

            if (log.isDetailed())
                logDetailed("Opened new file with name [" + filename + "]");
        }
    } catch (Exception e) {
        throw new KettleException("Error opening new file : " + e.toString());
    }
    // System.out.println("end of newFile(), splitnr="+splitnr);

    data.splitnr++;

    if (resultFile != null && meta.isAddToResultFiles()) {
        // Add this to the result file names...
        addResultFile(resultFile);
    }
}