/**
 * Finish the current PDF stream (writes the deferred length, too)
 */
void PDF_PLOTTER::closePdfStream()
{
    wxASSERT( workFile );

    long stream_len = ftell( workFile );

    if( stream_len < 0 )
    {
        wxASSERT( false );
        return;
    }

    // Rewind the file, read in the page stream and DEFLATE it
    fseek( workFile, 0, SEEK_SET );
    unsigned char *inbuf = new unsigned char[stream_len];

    int rc = fread( inbuf, 1, stream_len, workFile );
    wxASSERT( rc == stream_len );
    (void) rc;

    // We are done with the temporary file, junk it
    fclose( workFile );
    workFile = 0;
    ::wxRemoveFile( workFilename );

    // NULL means memos owns the memory, but provide a hint on optimum size needed.
    wxMemoryOutputStream    memos( NULL, std::max( 2000l, stream_len ) ) ;

    {
        /* Somewhat standard parameters to compress in DEFLATE. The PDF spec is
         * misleading, it says it wants a DEFLATE stream but it really want a ZLIB
         * stream! (a DEFLATE stream would be generated with -15 instead of 15)
         * rc = deflateInit2( &zstrm, Z_BEST_COMPRESSION, Z_DEFLATED, 15,
         *                    8, Z_DEFAULT_STRATEGY );
         */

        wxZlibOutputStream      zos( memos, wxZ_BEST_COMPRESSION, wxZLIB_ZLIB );

        zos.Write( inbuf, stream_len );

        delete[] inbuf;

    }   // flush the zip stream using zos destructor

    wxStreamBuffer* sb = memos.GetOutputStreamBuffer();

    unsigned out_count = sb->Tell();

    fwrite( sb->GetBufferStart(), 1, out_count, outputFile );

    fputs( "endstream\n", outputFile );
    closePdfObject();

    // Writing the deferred length as an indirect object
    startPdfObject( streamLengthHandle );
    fprintf( outputFile, "%u\n", out_count );
    closePdfObject();
}
Example #2
0
bool wxDebugReportCompress::DoProcess()
{
    const size_t count = GetFilesCount();
    if ( !count )
        return false;

    // create the compressed report file outside of the directory with the
    // report files as it will be deleted by wxDebugReport dtor but we want to
    // keep this one: for this we simply treat the directory name as the name
    // of the file so that its last component becomes our base name
    wxFileName fn(GetDirectory());
    if ( !m_zipDir.empty() )
        fn.SetPath(m_zipDir);
    if ( !m_zipName.empty() )
        fn.SetName(m_zipName);
    fn.SetExt("zip");

    // create the streams
    wxFFileOutputStream os(fn.GetFullPath(), wxT("wb"));
    wxZipOutputStream zos(os, 9);

    // add all files to the ZIP one
    wxString name, desc;
    for ( size_t n = 0; n < count; n++ )
    {
        GetFile(n, &name, &desc);

        wxZipEntry *ze = new wxZipEntry(name);
        ze->SetComment(desc);

        if ( !zos.PutNextEntry(ze) )
            return false;

        const wxFileName filename(GetDirectory(), name);
        wxFFileInputStream is(filename.GetFullPath());
        if ( !is.IsOk() || !zos.Write(is).IsOk() )
            return false;
    }

    if ( !zos.Close() )
        return false;

    m_zipfile = fn.GetFullPath();

    return true;
}
Example #3
0
bool wxDebugReportCompress::DoProcess()
{
    const size_t count = GetFilesCount();
    if ( !count )
        return false;

    // create the streams
    wxFileName fn(GetDirectory(), GetReportName(), _T("zip"));
    wxFFileOutputStream os(fn.GetFullPath(), _T("wb"));
    wxZipOutputStream zos(os, 9);

    // add all files to the ZIP one
    wxString name, desc;
    for ( size_t n = 0; n < count; n++ )
    {
        GetFile(n, &name, &desc);

        wxZipEntry *ze = new wxZipEntry(name);
        ze->SetComment(desc);

        if ( !zos.PutNextEntry(ze) )
            return false;

        wxFileName filename(fn.GetPath(), name);
        wxFFileInputStream is(filename.GetFullPath());
        if ( !is.IsOk() || !zos.Write(is).IsOk() )
            return false;
    }

    if ( !zos.Close() )
        return false;

    m_zipfile = fn.GetFullPath();

    return true;
}