11// ////////////////////////////////////////////////////////////////////////
22//
3- // Copyright (c) 2007-2010 , Image Engine Design Inc. All rights reserved.
3+ // Copyright (c) 2007-2015 , Image Engine Design Inc. All rights reserved.
44//
55// Redistribution and use in source and binary forms, with or without
66// modification, are permitted provided that the following conditions are
3232//
3333// ////////////////////////////////////////////////////////////////////////
3434
35+ #include " IECore/ObjectWriter.h"
3536#include " IECore/ObjectReader.h"
3637#include " IECore/FileIndexedIO.h"
38+ #include " IECore/MemoryIndexedIO.h"
3739#include " IECore/FileNameParameter.h"
3840#include " IECore/CompoundData.h"
3941
42+ #include " boost/filesystem/convenience.hpp"
43+ #include " boost/iostreams/filter/gzip.hpp"
44+
4045#include < cassert>
4146
4247using namespace IECore ;
@@ -45,14 +50,15 @@ using namespace boost;
4550IE_CORE_DEFINERUNTIMETYPED ( ObjectReader );
4651
4752const Reader::ReaderDescription<ObjectReader> ObjectReader::g_readerDescription ( " cob" );
53+ const Reader::ReaderDescription<ObjectReader> ObjectReader::g_compressedReaderDescription ( " cobz" );
4854
4955ObjectReader::ObjectReader () :
50- Reader( " Reads instances of a single Object from a file with a .cob extension" )
56+ Reader( " Reads instances of a single Object from a file with a .cob or .cobz extension" )
5157{
5258}
5359
5460ObjectReader::ObjectReader ( const std::string &fileName ) :
55- Reader( " Reads instances of a single Object from a file with a .cob extension" )
61+ Reader( " Reads instances of a single Object from a file with a .cob or .cobz extension" )
5662{
5763 m_fileNameParameter->setTypedValue ( fileName );
5864}
@@ -91,7 +97,48 @@ bool ObjectReader::canRead( const std::string &fileName )
9197ObjectPtr ObjectReader::doOperation ( const CompoundObject * operands )
9298{
9399 IndexedIOPtr io = open (fileName ());
94- return Object::load ( io, " object" );
100+
101+ // is this file compressed?
102+ if ( boost::filesystem::extension ( boost::filesystem::path ( fileName () ) ) == " .cobz" )
103+ {
104+ IndexedIOPtr objectCompressed = io->subdirectory ( " objectCompressed" );
105+
106+ // read size of compressed data:
107+ size_t compressedSize;
108+ objectCompressed->read ( " size" , compressedSize );
109+
110+ // read the actual data
111+ std::vector<char > compressedData ( compressedSize );
112+ char * cd = compressedData.data ();
113+ objectCompressed->read ( InternedString ( " data" ), cd, compressedSize * sizeof (char ) );
114+
115+ // decompress into a buffer for a MemoryIndexedIO:
116+ CharVectorDataPtr memBufferData = new CharVectorData;
117+
118+ // read compression type:
119+ std::string compressionType;
120+ objectCompressed->read ( " compressionType" , compressionType );
121+
122+ if ( compressionType == " gzip" )
123+ {
124+ boost::iostreams::filtering_ostream decompressingStream;
125+ decompressingStream.push ( boost::iostreams::gzip_decompressor () );
126+ decompressingStream.push ( boost::iostreams::back_inserter ( memBufferData->writable () ) );
127+ decompressingStream.write ( compressedData.data (), compressedData.size () * sizeof (char ) );
128+ }
129+ else
130+ {
131+ throw IECore::Exception ( " ObjectReader::doOperation: unrecognized compression type " + compressionType );
132+ }
133+
134+ MemoryIndexedIOPtr mio = new MemoryIndexedIO ( memBufferData, IndexedIO::rootPath, IndexedIO::Read );
135+
136+ return Object::load ( mio, " object" );
137+ }
138+ else
139+ {
140+ return Object::load ( io, " object" );
141+ }
95142}
96143
97144CompoundObjectPtr ObjectReader::readHeader ()
0 commit comments