Search in sources :

Example 1 with ZIPCompressionProvider

use of org.pentaho.di.core.compress.zip.ZIPCompressionProvider in project pentaho-kettle by pentaho.

the class TextFileOutput method initFileStreamWriter.

public void initFileStreamWriter(String filename) throws KettleException {
    data.writer = null;
    try {
        BufferedOutputStream bufferedOutputStream;
        OutputStream fileOutputStream;
        CompressionOutputStream compressionOutputStream;
        TextFileOutputData.FileStream fileStreams = null;
        try {
            if (meta.getSplitEvery() > 0) {
                if (filename.equals(data.getFileStreamsCollection().getLastFileName())) {
                    fileStreams = data.getFileStreamsCollection().getLastStream();
                }
            } else {
                fileStreams = data.getFileStreamsCollection().getStream(filename);
            }
            boolean writingToFileForFirstTime = fileStreams != null;
            boolean createParentDirIfNotExists = meta.isCreateParentFolder();
            boolean appendToExistingFile = meta.isFileAppended();
            if (fileStreams == null) {
                // Opening file for first time
                CompressionProvider compressionProvider = getCompressionProvider();
                boolean isZipFile = compressionProvider instanceof ZIPCompressionProvider;
                if (appendToExistingFile && isZipFile && isFileExists(filename)) {
                    throw new KettleException("Can not append to an existing zip file : " + filename);
                }
                int maxOpenFiles = getMaxOpenFiles();
                if ((maxOpenFiles > 0) && (data.getFileStreamsCollection().getNumOpenFiles() >= maxOpenFiles)) {
                    // If the file we're going to close is a zip file,  going to remove it from the collection of files
                    // that have been opened. We do this because it is not possible to reopen a
                    // zip file for append. By removing it from the collection, if the same file is referenced later, it will look
                    // like we're opening the file for the first time, and if we're set up to append to existing files it will cause and
                    // exception to be thrown, which is the desired result.
                    data.getFileStreamsCollection().closeOldestOpenFile(isZipFile);
                }
                if (createParentDirIfNotExists && ((data.getFileStreamsCollection().size() == 0) || meta.isFileNameInField())) {
                    createParentFolder(filename);
                }
                if (log.isDetailed()) {
                    logDetailed("Opening output stream using provider: " + compressionProvider.getName());
                }
                fileOutputStream = getOutputStream(filename, getTransMeta(), !isZipFile && appendToExistingFile);
                compressionOutputStream = compressionProvider.createOutputStream(fileOutputStream);
                // The compression output stream may also archive entries. For this we create the filename
                // (with appropriate extension) and add it as an entry to the output stream. For providers
                // that do not archive entries, they should use the default no-op implementation.
                compressionOutputStream.addEntry(filename, environmentSubstitute(meta.getExtension()));
                if (log.isDetailed()) {
                    if (!Utils.isEmpty(meta.getEncoding())) {
                        logDetailed("Opening output stream in encoding: " + meta.getEncoding());
                    } else {
                        logDetailed("Opening output stream in default encoding");
                    }
                }
                bufferedOutputStream = new BufferedOutputStream(compressionOutputStream, 5000);
                fileStreams = data.new FileStream(fileOutputStream, compressionOutputStream, bufferedOutputStream);
                data.getFileStreamsCollection().add(filename, fileStreams);
                if (log.isDetailed()) {
                    logDetailed("Opened new file with name [" + KettleVFS.getFriendlyURI(filename) + "]");
                }
            } else if (fileStreams.getBufferedOutputStream() == null) {
                // File was previously opened and now needs to be reopened.
                int maxOpenFiles = getMaxOpenFiles();
                if ((maxOpenFiles > 0) && (data.getFileStreamsCollection().getNumOpenFiles() >= maxOpenFiles)) {
                    data.getFileStreamsCollection().closeOldestOpenFile(false);
                }
                fileOutputStream = getOutputStream(filename, getTransMeta(), true);
                CompressionProvider compressionProvider = getCompressionProvider();
                compressionOutputStream = compressionProvider.createOutputStream(fileOutputStream);
                compressionOutputStream.addEntry(filename, environmentSubstitute(meta.getExtension()));
                bufferedOutputStream = new BufferedOutputStream(compressionOutputStream, 5000);
                fileStreams.setFileOutputStream(fileOutputStream);
                fileStreams.setCompressedOutputStream(compressionOutputStream);
                fileStreams.setBufferedOutputStream(bufferedOutputStream);
            }
            if (writingToFileForFirstTime) {
                if (meta.isAddToResultFiles()) {
                    // Add this to the result file names...
                    ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, getFileObject(filename, getTransMeta()), getTransMeta().getName(), getStepname());
                    if (resultFile != null) {
                        resultFile.setComment(BaseMessages.getString(PKG, "TextFileOutput.AddResultFile"));
                        addResultFile(resultFile);
                    }
                }
            }
        } catch (Exception e) {
            if (!(e instanceof KettleException)) {
                throw new KettleException("Error opening new file : " + e.toString());
            } else {
                throw (KettleException) e;
            }
        }
        fileStreams.setDirty(true);
        data.fos = fileStreams.getFileOutputStream();
        data.out = fileStreams.getCompressedOutputStream();
        data.writer = fileStreams.getBufferedOutputStream();
    } catch (KettleException ke) {
        throw ke;
    } catch (Exception e) {
        throw new KettleException("Error opening new file : " + e.toString());
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) CompressionOutputStream(org.pentaho.di.core.compress.CompressionOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) CompressionOutputStream(org.pentaho.di.core.compress.CompressionOutputStream) WriterOutputStream(org.pentaho.di.core.WriterOutputStream) ZIPCompressionProvider(org.pentaho.di.core.compress.zip.ZIPCompressionProvider) ResultFile(org.pentaho.di.core.ResultFile) KettleException(org.pentaho.di.core.exception.KettleException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) IOException(java.io.IOException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CompressionProvider(org.pentaho.di.core.compress.CompressionProvider) ZIPCompressionProvider(org.pentaho.di.core.compress.zip.ZIPCompressionProvider) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

BufferedOutputStream (java.io.BufferedOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ResultFile (org.pentaho.di.core.ResultFile)1 WriterOutputStream (org.pentaho.di.core.WriterOutputStream)1 CompressionOutputStream (org.pentaho.di.core.compress.CompressionOutputStream)1 CompressionProvider (org.pentaho.di.core.compress.CompressionProvider)1 ZIPCompressionProvider (org.pentaho.di.core.compress.zip.ZIPCompressionProvider)1 KettleException (org.pentaho.di.core.exception.KettleException)1 KettleFileException (org.pentaho.di.core.exception.KettleFileException)1 KettleStepException (org.pentaho.di.core.exception.KettleStepException)1 KettleValueException (org.pentaho.di.core.exception.KettleValueException)1