Merge pull request #1598 from assimp/export_3mf

Export 3mf
pull/1602/head^2
Kim Kulling 2017-11-30 10:13:36 +01:00 committed by GitHub
commit 4fe91f7a5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 6843 additions and 264 deletions

View File

@ -39,7 +39,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------
*/
#ifndef ASSIMP_BUILD_NO_EXPORT
#ifndef ASSIMP_BUILD_NO_3DS_EXPORTER
@ -210,6 +209,12 @@ Discreet3DSExporter:: Discreet3DSExporter(std::shared_ptr<IOStream> outfile, con
}
}
// ------------------------------------------------------------------------------------------------
Discreet3DSExporter::~Discreet3DSExporter() {
// empty
}
// ------------------------------------------------------------------------------------------------
int Discreet3DSExporter::WriteHierarchy(const aiNode& node, int seq, int sibling_level)
{

View File

@ -60,23 +60,21 @@ namespace Assimp
{
// ------------------------------------------------------------------------------------------------
/** Helper class to export a given scene to a 3DS file. */
/**
* @brief Helper class to export a given scene to a 3DS file.
*/
// ------------------------------------------------------------------------------------------------
class Discreet3DSExporter
{
class Discreet3DSExporter {
public:
Discreet3DSExporter(std::shared_ptr<IOStream> outfile, const aiScene* pScene);
~Discreet3DSExporter();
private:
void WriteMeshes();
void WriteMaterials();
void WriteTexture(const aiMaterial& mat, aiTextureType type, uint16_t chunk_flags);
void WriteFaceMaterialChunk(const aiMesh& mesh);
int WriteHierarchy(const aiNode& node, int level, int sibling_level);
void WriteString(const std::string& s);
void WriteString(const aiString& s);
void WriteColor(const aiColor3D& color);
@ -84,7 +82,6 @@ private:
void WritePercentChunk(double f);
private:
const aiScene* const scene;
StreamWriterLE writer;
@ -95,6 +92,6 @@ private:
};
}
} // Namespace Assimp
#endif
#endif // AI_3DSEXPORTER_H_INC

View File

@ -44,7 +44,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef AI_3DSFILEHELPER_H_INC
#define AI_3DSFILEHELPER_H_INC
#include "SpatialSort.h"
#include "SmoothingGroups.h"
#include "StringUtils.h"
@ -64,16 +63,19 @@ namespace D3DS {
/** Discreet3DS class: Helper class for loading 3ds files. Defines chunks
* and data structures.
*/
class Discreet3DS
{
class Discreet3DS {
private:
inline Discreet3DS() {}
Discreet3DS() {
// empty
}
~Discreet3DS() {
// empty
}
public:
//! data structure for a single chunk in a .3ds file
struct Chunk
{
struct Chunk {
uint16_t Flag;
uint32_t Size;
} PACK_STRUCT;

89
code/3MFXmlTags.h 100644
View File

@ -0,0 +1,89 @@
/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------
Copyright (c) 2006-2017, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of the assimp team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp team.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------
*/
#pragma once
namespace Assimp {
namespace D3MF {
namespace XmlTag {
static const std::string model = "model";
static const std::string model_unit = "unit";
static const std::string metadata = "metadata";
static const std::string resources = "resources";
static const std::string object = "object";
static const std::string mesh = "mesh";
static const std::string vertices = "vertices";
static const std::string vertex = "vertex";
static const std::string triangles = "triangles";
static const std::string triangle = "triangle";
static const std::string x = "x";
static const std::string y = "y";
static const std::string z = "z";
static const std::string v1 = "v1";
static const std::string v2 = "v2";
static const std::string v3 = "v3";
static const std::string id = "id";
static const std::string name = "name";
static const std::string type = "type";
static const std::string build = "build";
static const std::string item = "item";
static const std::string objectid = "objectid";
static const std::string transform = "transform";
static const std::string CONTENT_TYPES_ARCHIVE = "[Content_Types].xml";
static const std::string ROOT_RELATIONSHIPS_ARCHIVE = "_rels/.rels";
static const std::string SCHEMA_CONTENTTYPES = "http://schemas.openxmlformats.org/package/2006/content-types";
static const std::string SCHEMA_RELATIONSHIPS = "http://schemas.openxmlformats.org/package/2006/relationships";
static const std::string RELS_RELATIONSHIP_CONTAINER = "Relationships";
static const std::string RELS_RELATIONSHIP_NODE = "Relationship";
static const std::string RELS_ATTRIB_TARGET = "Target";
static const std::string RELS_ATTRIB_TYPE = "Type";
static const std::string RELS_ATTRIB_ID = "Id";
static const std::string PACKAGE_START_PART_RELATIONSHIP_TYPE = "http://schemas.microsoft.com/3dmanufacturing/2013/01/3dmodel";
static const std::string PACKAGE_PRINT_TICKET_RELATIONSHIP_TYPE = "http://schemas.microsoft.com/3dmanufacturing/2013/01/printticket";
static const std::string PACKAGE_TEXTURE_RELATIONSHIP_TYPE = "http://schemas.microsoft.com/3dmanufacturing/2013/01/3dtexture";
static const std::string PACKAGE_CORE_PROPERTIES_RELATIONSHIP_TYPE = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties";
static const std::string PACKAGE_THUMBNAIL_RELATIONSHIP_TYPE = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail";
}
} // Namespace D3MF
} // Namespace Assimp

View File

@ -677,8 +677,11 @@ ADD_ASSIMP_IMPORTER( GLTF
ADD_ASSIMP_IMPORTER( 3MF
D3MFImporter.h
D3MFImporter.cpp
D3MFExporter.h
D3MFExporter.cpp
D3MFOpcPackage.h
D3MFOpcPackage.cpp
3MFXmlTags.h
)
ADD_ASSIMP_IMPORTER( MMD
@ -740,6 +743,14 @@ SET( unzip_SRCS
)
SOURCE_GROUP( unzip FILES ${unzip_SRCS})
SET( ziplib_SRCS
../contrib/zip/src/miniz.h
../contrib/zip/src/zip.c
../contrib/zip/src/zip.h
)
SOURCE_GROUP( ziplib FILES ${ziplib_SRCS} )
SET ( openddl_parser_SRCS
../contrib/openddlparser/code/OpenDDLParser.cpp
../contrib/openddlparser/code/DDLNode.cpp
@ -851,6 +862,7 @@ SET( assimp_src
${Clipper_SRCS}
${openddl_parser_SRCS}
${open3dgc_SRCS}
${ziplib_SRCS}
# Necessary to show the headers in the project when using the VC++ generator:
${PUBLIC_HEADERS}

View File

@ -0,0 +1,285 @@
/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------
Copyright (c) 2006-2017, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of the assimp team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp team.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------
*/
#ifndef ASSIMP_BUILD_NO_EXPORT
#ifndef ASSIMP_BUILD_NO_3MF_EXPORTER
#include "D3MFExporter.h"
#include <assimp/scene.h>
#include <assimp/IOSystem.hpp>
#include <assimp/IOStream.hpp>
#include <assimp/Exporter.hpp>
#include <assimp/DefaultLogger.hpp>
#include "Exceptional.h"
#include "3MFXmlTags.h"
#include "D3MFOpcPackage.h"
#include <contrib/zip/src/zip.h>
namespace Assimp {
void ExportScene3MF( const char* pFile, IOSystem* pIOSystem, const aiScene* pScene, const ExportProperties* /*pProperties*/ ) {
if ( nullptr == pIOSystem ) {
throw DeadlyExportError( "Could not export 3MP archive: " + std::string( pFile ) );
}
D3MF::D3MFExporter myExporter( pFile, pScene );
if ( myExporter.validate() ) {
bool ok = myExporter.exportArchive(pFile);
if ( !ok ) {
throw DeadlyExportError( "Could not export 3MP archive: " + std::string( pFile ) );
}
}
}
namespace D3MF {
D3MFExporter::D3MFExporter( const char* pFile, const aiScene* pScene )
: mArchiveName( pFile )
, m_zipArchive( nullptr )
, mScene( pScene )
, mBuildItems()
, mRelations() {
// empty
}
D3MFExporter::~D3MFExporter() {
for ( size_t i = 0; i < mRelations.size(); ++i ) {
delete mRelations[ i ];
}
mRelations.clear();
}
bool D3MFExporter::validate() {
if ( mArchiveName.empty() ) {
return false;
}
if ( nullptr == mScene ) {
return false;
}
return true;
}
bool D3MFExporter::exportArchive( const char *file ) {
bool ok( true );
m_zipArchive = zip_open( file, ZIP_DEFAULT_COMPRESSION_LEVEL, 'w' );
if ( nullptr == m_zipArchive ) {
return false;
}
ok |= export3DModel();
ok |= exportRelations();
zip_close( m_zipArchive );
m_zipArchive = nullptr;
return ok;
}
bool D3MFExporter::exportRelations() {
mRelOutput.clear();
mRelOutput << "<?xml version = \"1.0\" encoding = \"UTF-8\"?>\n";
mRelOutput << "<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">\n";
for ( size_t i = 0; i < mRelations.size(); ++i ) {
mRelOutput << "<Relationship Target =\"/3D/" << mRelations[ i ]->target << "\" ";
mRelOutput << "id=\"" << mRelations[i]->id << "\" ";
mRelOutput << "Type=\"" << mRelations[ i ]->type << "/>";
mRelOutput << std::endl;
}
mRelOutput << "</Relationships>";
mRelOutput << std::endl;
writeRelInfoToFile( "_rels", ".rels" );
mRelOutput.flush();
return true;
}
bool D3MFExporter::export3DModel() {
mModelOutput.clear();
writeHeader();
mModelOutput << "<" << XmlTag::model << " " << XmlTag::model_unit << "=\"millimeter\""
<< "xmlns=\"http://schemas.microsoft.com/3dmanufacturing/core/2015/02\">"
<< std::endl;
mModelOutput << "<" << XmlTag::resources << ">";
mModelOutput << std::endl;
writeObjects();
mModelOutput << "</" << XmlTag::resources << ">";
mModelOutput << std::endl;
writeBuild();
mModelOutput << "</" << XmlTag::model << ">\n";
OpcPackageRelationship *info = new OpcPackageRelationship;
info->id = mArchiveName;
info->target = "rel0";
info->type = "http://schemas.microsoft.com/3dmanufacturing/2013/01/3dmodel";
mRelations.push_back( info );
writeModelToArchive( "3D", "3DModel.model" );
mModelOutput.flush();
return true;
}
void D3MFExporter::writeHeader() {
mModelOutput << "<?xml version=\"1.0\" encoding=\"UTF - 8\"?>";
mModelOutput << std::endl;
}
void D3MFExporter::writeObjects() {
if ( nullptr == mScene->mRootNode ) {
return;
}
aiNode *root = mScene->mRootNode;
for ( unsigned int i = 0; i < root->mNumChildren; ++i ) {
aiNode *currentNode( root->mChildren[ i ] );
if ( nullptr == currentNode ) {
continue;
}
mModelOutput << "<" << XmlTag::object << " id=\"" << currentNode->mName.C_Str() << "\" type=\"model\">";
mModelOutput << std::endl;
for ( unsigned int j = 0; j < currentNode->mNumMeshes; ++j ) {
aiMesh *currentMesh = mScene->mMeshes[ currentNode->mMeshes[ j ] ];
if ( nullptr == currentMesh ) {
continue;
}
writeMesh( currentMesh );
}
mBuildItems.push_back( i );
mModelOutput << "</" << XmlTag::object << ">";
mModelOutput << std::endl;
}
}
void D3MFExporter::writeMesh( aiMesh *mesh ) {
if ( nullptr == mesh ) {
return;
}
mModelOutput << "<" << XmlTag::mesh << ">" << std::endl;
mModelOutput << "<" << XmlTag::vertices << ">" << std::endl;
for ( unsigned int i = 0; i < mesh->mNumVertices; ++i ) {
writeVertex( mesh->mVertices[ i ] );
}
mModelOutput << "</" << XmlTag::vertices << ">" << std::endl;
mModelOutput << "</" << XmlTag::mesh << ">" << std::endl;
writeFaces( mesh );
}
void D3MFExporter::writeVertex( const aiVector3D &pos ) {
mModelOutput << "<" << XmlTag::vertex << " x=\"" << pos.x << "\" y=\"" << pos.y << "\" z=\"" << pos.z << "\">";
mModelOutput << std::endl;
}
void D3MFExporter::writeFaces( aiMesh *mesh ) {
if ( nullptr == mesh ) {
return;
}
if ( !mesh->HasFaces() ) {
return;
}
mModelOutput << "<" << XmlTag::triangles << ">" << std::endl;
for ( unsigned int i = 0; i < mesh->mNumFaces; ++i ) {
aiFace &currentFace = mesh->mFaces[ i ];
mModelOutput << "<" << XmlTag::triangle << " v1=\"" << currentFace.mIndices[ 0 ] << "\" v2=\""
<< currentFace.mIndices[ 1 ] << "\" v3=\"" << currentFace.mIndices[ 2 ] << "\"/>";
mModelOutput << std::endl;
}
mModelOutput << "</" << XmlTag::triangles << ">";
mModelOutput << std::endl;
}
void D3MFExporter::writeBuild() {
mModelOutput << "<" << XmlTag::build << ">" << std::endl;
for ( size_t i = 0; i < mBuildItems.size(); ++i ) {
mModelOutput << "<" << XmlTag::item << " objectid=\"" << i + 1 << "\"/>";
mModelOutput << std::endl;
}
mModelOutput << "</" << XmlTag::build << ">";
mModelOutput << std::endl;
}
void D3MFExporter::writeModelToArchive( const std::string &folder, const std::string &modelName ) {
if ( nullptr == m_zipArchive ) {
throw DeadlyExportError( "3MF-Export: Zip archive not valid, nullptr." );
}
const std::string entry = folder + "/" + modelName;
zip_entry_open( m_zipArchive, entry.c_str() );
const std::string &exportTxt( mModelOutput.str() );
zip_entry_write( m_zipArchive, exportTxt.c_str(), exportTxt.size() );
zip_entry_close( m_zipArchive );
}
void D3MFExporter::writeRelInfoToFile( const std::string &folder, const std::string &relName ) {
if ( nullptr == m_zipArchive ) {
throw DeadlyExportError( "3MF-Export: Zip archive not valid, nullptr." );
}
const std::string entry = folder + "/" + relName;
zip_entry_open( m_zipArchive, entry.c_str() );
const std::string &exportTxt( mRelOutput.str() );
zip_entry_write( m_zipArchive, exportTxt.c_str(), exportTxt.size() );
zip_entry_close( m_zipArchive );
}
} // Namespace D3MF
} // Namespace Assimp
#endif // ASSIMP_BUILD_NO3MF_EXPORTER
#endif // ASSIMP_BUILD_NO_EXPORT

100
code/D3MFExporter.h 100644
View File

@ -0,0 +1,100 @@
/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------
Copyright (c) 2006-2017, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of the assimp team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp team.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------
*/
#pragma once
#include <memory>
#include <sstream>
#include <vector>
#include <assimp/vector3.h>
struct aiScene;
struct aiNode;
struct aiMaterial;
struct aiMesh;
struct zip_t;
namespace Assimp {
class IOStream;
namespace D3MF {
#ifndef ASSIMP_BUILD_NO_EXPORT
#ifndef ASSIMP_BUILD_NO3MF_EXPORTER
struct OpcPackageRelationship;
class D3MFExporter {
public:
D3MFExporter( const char* pFile, const aiScene* pScene );
~D3MFExporter();
bool validate();
bool exportArchive( const char *file );
bool exportRelations();
bool export3DModel();
protected:
void writeHeader();
void writeObjects();
void writeMesh( aiMesh *mesh );
void writeVertex( const aiVector3D &pos );
void writeFaces( aiMesh *mesh );
void writeBuild();
void writeModelToArchive( const std::string &folder, const std::string &modelName );
void writeRelInfoToFile( const std::string &folder, const std::string &relName );
private:
std::string mArchiveName;
zip_t *m_zipArchive;
const aiScene *mScene;
std::ostringstream mModelOutput;
std::ostringstream mRelOutput;
std::vector<unsigned int> mBuildItems;
std::vector<OpcPackageRelationship*> mRelations;
};
#endif // ASSIMP_BUILD_NO3MF_EXPORTER
#endif // ASSIMP_BUILD_NO_EXPORT
} // Namespace D3MF
} // Namespace Assimp

View File

@ -59,65 +59,37 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "D3MFOpcPackage.h"
#include <contrib/unzip/unzip.h>
#include "irrXMLWrapper.h"
#include "3MFXmlTags.h"
namespace Assimp {
namespace D3MF {
namespace XmlTag {
static const std::string model = "model";
static const std::string metadata = "metadata";
static const std::string resources = "resources";
static const std::string object = "object";
static const std::string mesh = "mesh";
static const std::string vertices = "vertices";
static const std::string vertex = "vertex";
static const std::string triangles = "triangles";
static const std::string triangle = "triangle";
static const std::string x = "x";
static const std::string y = "y";
static const std::string z = "z";
static const std::string v1 = "v1";
static const std::string v2 = "v2";
static const std::string v3 = "v3";
static const std::string id = "id";
static const std::string name = "name";
static const std::string type = "type";
static const std::string build = "build";
static const std::string item = "item";
static const std::string objectid = "objectid";
static const std::string transform = "transform";
}
class XmlSerializer
{
class XmlSerializer {
public:
XmlSerializer(XmlReader* xmlReader)
: xmlReader(xmlReader)
{
: xmlReader(xmlReader) {
// empty
}
~XmlSerializer() {
}
void ImportXml(aiScene* scene) {
scene->mRootNode = new aiNode();
std::vector<aiNode*> children;
while(ReadToEndElement(D3MF::XmlTag::model))
{
if(xmlReader->getNodeName() == D3MF::XmlTag::object)
{
while(ReadToEndElement(D3MF::XmlTag::model)) {
if(xmlReader->getNodeName() == D3MF::XmlTag::object) {
children.push_back(ReadObject(scene));
}
else if(xmlReader->getNodeName() == D3MF::XmlTag::build)
{
} else if(xmlReader->getNodeName() == D3MF::XmlTag::build) {
}
}
if(scene->mRootNode->mName.length == 0)
if ( scene->mRootNode->mName.length == 0 ) {
scene->mRootNode->mName.Set( "3MF" );
}
scene->mNumMeshes = static_cast<unsigned int>(meshes.size());
scene->mMeshes = new aiMesh*[scene->mNumMeshes]();
@ -178,10 +150,8 @@ private:
}
aiMesh* ReadMesh()
{
aiMesh* ReadMesh() {
aiMesh* mesh = new aiMesh();
while(ReadToEndElement(D3MF::XmlTag::mesh))
{
if(xmlReader->getNodeName() == D3MF::XmlTag::vertices)
@ -192,10 +162,8 @@ private:
{
ImportTriangles(mesh);
}
}
return mesh;
}
@ -216,6 +184,7 @@ private:
std::copy(vertices.begin(), vertices.end(), mesh->mVertices);
}
aiVector3D ReadVertex()
{
aiVector3D vertex;
@ -261,7 +230,6 @@ private:
}
private:
bool ReadToStartElement(const std::string& startTag)
{
while(xmlReader->read())
@ -305,6 +273,7 @@ private:
} //namespace D3MF
static const std::string Extension = "3mf";
static const aiImporterDesc desc = {
"3mf Importer",
@ -316,24 +285,22 @@ static const aiImporterDesc desc = {
0,
0,
0,
"3mf"
Extension.c_str()
};
D3MFImporter::D3MFImporter()
{
: BaseImporter() {
// empty
}
D3MFImporter::~D3MFImporter()
{
D3MFImporter::~D3MFImporter() {
// empty
}
bool D3MFImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const
{
bool D3MFImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const {
const std::string extension = GetExtension(pFile);
if(extension == "3mf") {
if(extension == Extension ) {
return true;
} else if ( !extension.length() || checkSig ) {
if (nullptr == pIOHandler ) {
@ -344,18 +311,15 @@ bool D3MFImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool
return false;
}
void D3MFImporter::SetupProperties(const Importer * /*pImp*/)
{
void D3MFImporter::SetupProperties(const Importer * /*pImp*/) {
// empty
}
const aiImporterDesc *D3MFImporter::GetInfo() const
{
const aiImporterDesc *D3MFImporter::GetInfo() const {
return &desc;
}
void D3MFImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler)
{
void D3MFImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
D3MF::D3MFOpcPackage opcPackage(pIOHandler, pFile);
std::unique_ptr<CIrrXML_IOStreamReader> xmlStream(new CIrrXML_IOStreamReader(opcPackage.RootStream()));

View File

@ -46,21 +46,19 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace Assimp {
class D3MFImporter : public BaseImporter
{
class D3MFImporter : public BaseImporter {
public:
// BaseImporter interface
D3MFImporter();
~D3MFImporter();
// BaseImporter interface
public:
bool CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const;
void SetupProperties(const Importer *pImp);
const aiImporterDesc *GetInfo() const;
protected:
void InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler);
};
}
} // Namespace Assimp
#endif // AI_D3MFLOADER_H_INCLUDED

View File

@ -55,48 +55,22 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <map>
#include <algorithm>
#include <cassert>
#include <contrib/unzip/unzip.h>
#include "3MFXmlTags.h"
namespace Assimp {
namespace D3MF {
namespace XmlTag {
static const std::string CONTENT_TYPES_ARCHIVE = "[Content_Types].xml";
static const std::string ROOT_RELATIONSHIPS_ARCHIVE = "_rels/.rels";
static const std::string SCHEMA_CONTENTTYPES = "http://schemas.openxmlformats.org/package/2006/content-types";
static const std::string SCHEMA_RELATIONSHIPS = "http://schemas.openxmlformats.org/package/2006/relationships";
static const std::string RELS_RELATIONSHIP_CONTAINER = "Relationships";
static const std::string RELS_RELATIONSHIP_NODE = "Relationship";
static const std::string RELS_ATTRIB_TARGET = "Target";
static const std::string RELS_ATTRIB_TYPE = "Type";
static const std::string RELS_ATTRIB_ID = "Id";
static const std::string PACKAGE_START_PART_RELATIONSHIP_TYPE = "http://schemas.microsoft.com/3dmanufacturing/2013/01/3dmodel";
static const std::string PACKAGE_PRINT_TICKET_RELATIONSHIP_TYPE = "http://schemas.microsoft.com/3dmanufacturing/2013/01/printticket";
static const std::string PACKAGE_TEXTURE_RELATIONSHIP_TYPE = "http://schemas.microsoft.com/3dmanufacturing/2013/01/3dtexture";
static const std::string PACKAGE_CORE_PROPERTIES_RELATIONSHIP_TYPE = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties";
static const std::string PACKAGE_THUMBNAIL_RELATIONSHIP_TYPE = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail";
}
class IOSystem2Unzip {
public:
static voidpf open(voidpf opaque, const char* filename, int mode);
static uLong read(voidpf opaque, voidpf stream, void* buf, uLong size);
static uLong write(voidpf opaque, voidpf stream, const void* buf, uLong size);
static long tell(voidpf opaque, voidpf stream);
static long seek(voidpf opaque, voidpf stream, uLong offset, int origin);
static int close(voidpf opaque, voidpf stream);
static int testerror(voidpf opaque, voidpf stream);
static zlib_filefunc_def get(IOSystem* pIOHandler);
};
@ -116,7 +90,6 @@ voidpf IOSystem2Unzip::open(voidpf opaque, const char* filename, int mode) {
}
}
return (voidpf) io_system->Open(filename, mode_fopen);
}
@ -186,44 +159,33 @@ zlib_filefunc_def IOSystem2Unzip::get(IOSystem* pIOHandler) {
return mapping;
}
class ZipFile : public IOStream
{
class ZipFile : public IOStream {
friend class D3MFZipArchive;
public:
explicit ZipFile(size_t size);
~ZipFile();
virtual ~ZipFile();
size_t Read(void* pvBuffer, size_t pSize, size_t pCount );
size_t Write(const void* /*pvBuffer*/, size_t /*pSize*/, size_t /*pCount*/);
size_t FileSize() const;
aiReturn Seek(size_t /*pOffset*/, aiOrigin /*pOrigin*/);
size_t Tell() const;
void Flush();
private:
void *m_Buffer;
size_t m_Size;
};
ZipFile::ZipFile(size_t size) : m_Size(size) {
ZipFile::ZipFile(size_t size)
: m_Buffer( nullptr )
, m_Size(size) {
ai_assert(m_Size != 0);
m_Buffer = malloc(m_Size);
m_Buffer = ::malloc(m_Size);
}
ZipFile::~ZipFile() {
free(m_Buffer);
::free(m_Buffer);
m_Buffer = NULL;
}
@ -236,8 +198,12 @@ size_t ZipFile::Read(void* pvBuffer, size_t pSize, size_t pCount) {
return size;
}
size_t ZipFile::Write(const void* /*pvBuffer*/, size_t /*pSize*/, size_t /*pCount*/) {
return 0;
size_t ZipFile::Write(const void* pvBuffer, size_t size, size_t pCount ) {
const size_t size_to_write( size * pCount );
if ( 0 == size_to_write ) {
return 0U;
}
return 0U;
}
size_t ZipFile::FileSize() const {
@ -256,55 +222,36 @@ void ZipFile::Flush() {
// empty
}
class D3MFZipArchive : public IOSystem
{
class D3MFZipArchive : public IOSystem {
public:
static const unsigned int FileNameSize = 256;
public:
D3MFZipArchive(IOSystem* pIOHandler, const std::string & rFile);
~D3MFZipArchive();
bool Exists(const char* pFile) const;
char getOsSeparator() const;
IOStream* Open(const char* pFile, const char* pMode = "rb");
void Close(IOStream* pFile);
bool isOpen() const;
void getFileList(std::vector<std::string> &rFileList);
private:
bool mapArchive();
private:
unzFile m_ZipFileHandle;
std::map<std::string, ZipFile*> m_ArchiveMap;
};
// ------------------------------------------------------------------------------------------------
// Constructor.
D3MFZipArchive::D3MFZipArchive(IOSystem* pIOHandler, const std::string& rFile)
: m_ZipFileHandle(NULL), m_ArchiveMap()
{
if (! rFile.empty())
{
: m_ZipFileHandle(NULL)
, m_ArchiveMap() {
if (! rFile.empty()) {
zlib_filefunc_def mapping = IOSystem2Unzip::get(pIOHandler);
m_ZipFileHandle = unzOpen2(rFile.c_str(), &mapping);
if(m_ZipFileHandle != NULL) {
mapArchive();
}
@ -434,24 +381,12 @@ bool D3MFZipArchive::mapArchive() {
// ------------------------------------------------------------------------------------------------
struct OpcPackageRelationship
{
std::string id;
std::string type;
std::string target;
};
typedef std::shared_ptr<OpcPackageRelationship> OpcPackageRelationshipPtr;
class OpcPackageRelationshipReader
{
class OpcPackageRelationshipReader {
public:
OpcPackageRelationshipReader(XmlReader* xmlReader)
{
while(xmlReader->read())
{
OpcPackageRelationshipReader(XmlReader* xmlReader) {
while(xmlReader->read()) {
if(xmlReader->getNodeType() == irr::io::EXN_ELEMENT &&
xmlReader->getNodeName() == XmlTag::RELS_RELATIONSHIP_CONTAINER)
{
@ -474,13 +409,11 @@ public:
}
}
void ParseAttributes(XmlReader*)
{
void ParseAttributes(XmlReader*) {
// empty
}
void ParseChildNode(XmlReader* xmlReader)
{
void ParseChildNode(XmlReader* xmlReader) {
OpcPackageRelationshipPtr relPtr(new OpcPackageRelationship());
relPtr->id = xmlReader->getAttributeValue(XmlTag::RELS_ATTRIB_ID.c_str());
@ -489,42 +422,41 @@ public:
m_relationShips.push_back(relPtr);
}
std::vector<OpcPackageRelationshipPtr> m_relationShips;
};
// ------------------------------------------------------------------------------------------------
D3MFOpcPackage::D3MFOpcPackage(IOSystem* pIOHandler, const std::string& rFile)
: m_RootStream(nullptr)
{
zipArchive.reset(new D3MF::D3MFZipArchive( pIOHandler, rFile ));
if(!zipArchive->isOpen()) {
: mRootStream(nullptr)
, mZipArchive() {
mZipArchive.reset( new D3MF::D3MFZipArchive( pIOHandler, rFile ) );
if(!mZipArchive->isOpen()) {
throw DeadlyImportError("Failed to open file " + rFile+ ".");
}
std::vector<std::string> fileList;
zipArchive->getFileList(fileList);
mZipArchive->getFileList(fileList);
for (auto& file: fileList) {
if(file == D3MF::XmlTag::ROOT_RELATIONSHIPS_ARCHIVE) {
//PkgRelationshipReader pkgRelReader(file, archive);
ai_assert(zipArchive->Exists(file.c_str()));
ai_assert(mZipArchive->Exists(file.c_str()));
IOStream *fileStream = zipArchive->Open(file.c_str());
IOStream *fileStream = mZipArchive->Open(file.c_str());
ai_assert(fileStream != nullptr);
std::string rootFile = ReadPackageRootRelationship(fileStream);
if(rootFile.size() > 0 && rootFile[0] == '/')
if ( rootFile.size() > 0 && rootFile[ 0 ] == '/' ) {
rootFile = rootFile.substr( 1 );
}
DefaultLogger::get()->debug(rootFile);
m_RootStream = zipArchive->Open(rootFile.c_str());
ai_assert(m_RootStream != nullptr);
mRootStream = mZipArchive->Open(rootFile.c_str());
ai_assert(mRootStream != nullptr);
// const size_t size = zipArchive->FileSize();
// m_Data.resize( size );
@ -535,50 +467,40 @@ D3MFOpcPackage::D3MFOpcPackage(IOSystem* pIOHandler, const std::string& rFile)
// m_Data.clear();
// return false;
// }
zipArchive->Close( fileStream );
mZipArchive->Close( fileStream );
}
else if( file == D3MF::XmlTag::CONTENT_TYPES_ARCHIVE)
{
} else if( file == D3MF::XmlTag::CONTENT_TYPES_ARCHIVE) {
}
}
}
D3MFOpcPackage::~D3MFOpcPackage()
{
D3MFOpcPackage::~D3MFOpcPackage() {
// empty
}
IOStream* D3MFOpcPackage::RootStream() const
{
return m_RootStream;
IOStream* D3MFOpcPackage::RootStream() const {
return mRootStream;
}
std::string D3MFOpcPackage::ReadPackageRootRelationship(IOStream* stream)
{
std::string D3MFOpcPackage::ReadPackageRootRelationship(IOStream* stream) {
std::unique_ptr<CIrrXML_IOStreamReader> xmlStream(new CIrrXML_IOStreamReader(stream));
std::unique_ptr<XmlReader> xml(irr::io::createIrrXMLReader(xmlStream.get()));
OpcPackageRelationshipReader reader(xml.get());
auto itr = std::find_if(reader.m_relationShips.begin(), reader.m_relationShips.end(), [](const OpcPackageRelationshipPtr& rel){
return rel->type == XmlTag::PACKAGE_START_PART_RELATIONSHIP_TYPE;
});
if(itr == reader.m_relationShips.end())
throw DeadlyImportError("Cannot find" + XmlTag::PACKAGE_START_PART_RELATIONSHIP_TYPE);
return (*itr)->target;
}
} //namespace D3MF
} // Namespace D3MF
}
} // Namespace Assimp
#endif //ASSIMP_BUILD_NO_3MF_IMPORTER

View File

@ -48,26 +48,31 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "irrXMLWrapper.h"
namespace Assimp {
namespace D3MF {
typedef irr::io::IrrXMLReader XmlReader;
typedef std::shared_ptr<XmlReader> XmlReaderPtr;
struct OpcPackageRelationship {
std::string id;
std::string type;
std::string target;
};
class D3MFZipArchive;
class D3MFOpcPackage
{
class D3MFOpcPackage {
public:
D3MFOpcPackage(IOSystem* pIOHandler, const std::string& rFile);
~D3MFOpcPackage();
IOStream* RootStream() const;
private:
protected:
std::string ReadPackageRootRelationship(IOStream* stream);
private:
IOStream* m_RootStream;
std::unique_ptr<D3MFZipArchive> zipArchive;
IOStream* mRootStream;
std::unique_ptr<D3MFZipArchive> mZipArchive;
};
}

View File

@ -95,6 +95,7 @@ void ExportSceneGLTF2(const char*, IOSystem*, const aiScene*, const ExportProper
void ExportSceneAssbin(const char*, IOSystem*, const aiScene*, const ExportProperties*);
void ExportSceneAssxml(const char*, IOSystem*, const aiScene*, const ExportProperties*);
void ExportSceneX3D(const char*, IOSystem*, const aiScene*, const ExportProperties*);
void ExportScene3MF( const char*, IOSystem*, const aiScene*, const ExportProperties* );
// ------------------------------------------------------------------------------------------------
// global array of all export formats which Assimp supports in its current build
@ -163,6 +164,10 @@ Exporter::ExportFormatEntry gExporters[] =
#ifndef ASSIMP_BUILD_NO_X3D_EXPORTER
Exporter::ExportFormatEntry( "x3d", "Extensible 3D", "x3d" , &ExportSceneX3D, 0 ),
#endif
#ifndef ASSIMP_BUILD_NO3MF_EXPORTER
Exporter::ExportFormatEntry( "3mf", "The 3MF-File-Format", "3mf", &ExportScene3MF, 0 )
#endif
};
#define ASSIMP_NUM_EXPORTERS (sizeof(gExporters)/sizeof(gExporters[0]))

View File

@ -715,6 +715,7 @@ public:
if (floatValue) {
return floatValue->value.size() == 1 ? floatValue->value.front() : 0;
}
return fast_atof(attr->value->toString().c_str());
}
@ -1793,6 +1794,7 @@ public:
virtual void registerDecoder(const std::string & /*algorithmUri*/, std::unique_ptr<FIDecoder> /*decoder*/) /*override*/ {}
virtual void registerVocabulary(const std::string &/*vocabularyUri*/, const FIVocabulary * /*vocabulary*/) /*override*/ {}
private:

View File

@ -70,9 +70,7 @@ namespace Assimp {
* }
* @endcode
**/
class CIrrXML_IOStreamReader
: public irr::io::IFileReadCallBack
{
class CIrrXML_IOStreamReader : public irr::io::IFileReadCallBack {
public:
// ----------------------------------------------------------------------------------

38
contrib/zip/.gitignore vendored 100644
View File

@ -0,0 +1,38 @@
/build/
/test/build/
/xcodeproj/
# Object files
*.o
*.ko
*.obj
*.elf
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
*.suo
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Temporary
*.swp
.DS_Store

View File

@ -0,0 +1,10 @@
language: c
# Compiler selection
compiler:
- clang
- gcc
# Build steps
script:
- mkdir build
- cd build
- cmake -DCMAKE_BUILD_TYPE=Debug .. && make && make test

View File

@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 2.8)
project(zip)
if (MSVC)
# Use secure functions by defaualt and suppress warnings about "deprecated" functions
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES=1")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT=1")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_NONSTDC_NO_WARNINGS=1 /D _CRT_SECURE_NO_WARNINGS=1")
endif (MSVC)
# zip
set(SRC src/miniz.h src/zip.h src/zip.c)
add_library(${CMAKE_PROJECT_NAME} ${SRC})
# test
enable_testing()
add_subdirectory(test)

View File

@ -0,0 +1,139 @@
### A portable (OSX/Linux/Windows), simple zip library written in C
This is done by hacking awesome [miniz](https://code.google.com/p/miniz) library and layering functions on top of the miniz v1.15 API.
[![Windows][win-badge]][win-link] [![OS X][osx-linux-badge]][osx-linux-link]
[win-badge]: https://img.shields.io/appveyor/ci/kuba--/zip/master.svg?label=windows "AppVeyor build status"
[win-link]: https://ci.appveyor.com/project/kuba--/zip "AppVeyor build status"
[osx-linux-badge]: https://img.shields.io/travis/kuba--/zip/master.svg?label=linux/osx "Travis CI build status"
[osx-linux-link]: https://travis-ci.org/kuba--/zip "Travis CI build status"
# The Idea
<img src="zip.png" name="zip" />
... Some day, I was looking for zip library written in C for my project, but I could not find anything simple enough and lightweight.
Everything what I tried required 'crazy mental gymnastics' to integrate or had some limitations or was too heavy.
I hate frameworks, factories and adding new dependencies. If I must to install all those dependencies and link new library, I'm getting almost sick.
I wanted something powerfull and small enough, so I could add just a few files and compile them into my project.
And finally I found miniz.
Miniz is a lossless, high performance data compression library in a single source file. I only needed simple interface to append buffers or files to the current zip-entry. Thanks to this feature I'm able to merge many files/buffers and compress them on-the-fly.
It was the reason, why I decided to write zip module on top of the miniz. It required a little bit hacking and wrapping some functions, but I kept simplicity. So, you can grab these 3 files and compile them into your project. I hope that interface is also extremely simple, so you will not have any problems to understand it.
# Examples
* Create a new zip archive with default compression level.
```c
struct zip_t *zip = zip_open("foo.zip", ZIP_DEFAULT_COMPRESSION_LEVEL, 'w');
{
zip_entry_open(zip, "foo-1.txt");
{
char *buf = "Some data here...";
zip_entry_write(zip, buf, strlen(buf));
}
zip_entry_close(zip);
zip_entry_open(zip, "foo-2.txt");
{
// merge 3 files into one entry and compress them on-the-fly.
zip_entry_fwrite(zip, "foo-2.1.txt");
zip_entry_fwrite(zip, "foo-2.2.txt");
zip_entry_fwrite(zip, "foo-2.3.txt");
}
zip_entry_close(zip);
}
zip_close(zip);
```
* Append to the existing zip archive.
```c
struct zip_t *zip = zip_open("foo.zip", ZIP_DEFAULT_COMPRESSION_LEVEL, 'a');
{
zip_entry_open(zip, "foo-3.txt");
{
char *buf = "Append some data here...";
zip_entry_write(zip, buf, strlen(buf));
}
zip_entry_close(zip);
}
zip_close(zip);
```
* Extract a zip archive into a folder.
```c
int on_extract_entry(const char *filename, void *arg) {
static int i = 0;
int n = *(int *)arg;
printf("Extracted: %s (%d of %d)\n", filename, ++i, n);
return 0;
}
int arg = 2;
zip_extract("foo.zip", "/tmp", on_extract_entry, &arg);
```
* Extract a zip entry into memory.
```c
void *buf = NULL;
size_t bufsize;
struct zip_t *zip = zip_open("foo.zip", 0, 'r');
{
zip_entry_open(zip, "foo-1.txt");
{
zip_entry_read(zip, &buf, &bufsize);
}
zip_entry_close(zip);
}
zip_close(zip);
free(buf);
```
* Extract a zip entry into memory using callback.
```c
struct buffer_t {
char *data;
size_t size;
};
static size_t on_extract(void *arg, unsigned long long offset, const void *data, size_t size) {
struct buffer_t *buf = (struct buffer_t *)arg;
buf->data = realloc(buf->data, buf->size + size + 1);
assert(NULL != buf->data);
memcpy(&(buf->data[buf->size]), data, size);
buf->size += size;
buf->data[buf->size] = 0;
return size;
}
struct buffer_t buf = {0};
struct zip_t *zip = zip_open("foo.zip", 0, 'r');
{
zip_entry_open(zip, "foo-1.txt");
{
zip_entry_extract(zip, on_extract, &buf);
}
zip_entry_close(zip);
}
zip_close(zip);
free(buf.data);
```
* Extract a zip entry into a file.
```c
struct zip_t *zip = zip_open("foo.zip", 0, 'r');
{
zip_entry_open(zip, "foo-2.txt");
{
zip_entry_fread(zip, "foo-2.txt");
}
zip_entry_close(zip);
}
zip_close(zip);
```

View File

@ -0,0 +1,26 @@
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/

View File

@ -0,0 +1,14 @@
version: 1.0.{build}
build_script:
- cmd: >-
cd c:\projects\zip
mkdir build
cd build
cmake -G"Visual Studio 14" -DCMAKE_BUILD_TYPE=Debug ..
cmake --build . --config %CMAKE_BUILD_TYPE%
ctest --verbose -C "Debug"

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,640 @@
/*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "zip.h"
#include "miniz.h"
#include <errno.h>
#include <sys/stat.h>
#include <time.h>
#if defined _WIN32 || defined __WIN32__
/* Win32, DOS */
#include <direct.h>
#define MKDIR(DIRNAME) _mkdir(DIRNAME)
#define STRCLONE(STR) ((STR) ? _strdup(STR) : NULL)
#define HAS_DEVICE(P) \
((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) && \
(P)[1] == ':')
#define FILESYSTEM_PREFIX_LEN(P) (HAS_DEVICE(P) ? 2 : 0)
#define ISSLASH(C) ((C) == '/' || (C) == '\\')
#else
#define MKDIR(DIRNAME) mkdir(DIRNAME, 0755)
#define STRCLONE(STR) ((STR) ? strdup(STR) : NULL)
#endif
#ifndef FILESYSTEM_PREFIX_LEN
#define FILESYSTEM_PREFIX_LEN(P) 0
#endif
#ifndef ISSLASH
#define ISSLASH(C) ((C) == '/')
#endif
#define CLEANUP(ptr) \
do { \
if (ptr) { \
free((void *)ptr); \
ptr = NULL; \
} \
} while (0)
static char *basename(const char *name) {
char const *p;
char const *base = name += FILESYSTEM_PREFIX_LEN(name);
int all_slashes = 1;
for (p = name; *p; p++) {
if (ISSLASH(*p))
base = p + 1;
else
all_slashes = 0;
}
/* If NAME is all slashes, arrange to return `/'. */
if (*base == '\0' && ISSLASH(*name) && all_slashes) --base;
return (char *)base;
}
static int mkpath(const char *path) {
char const *p;
char npath[MAX_PATH + 1] = {0};
int len = 0;
for (p = path; *p && len < MAX_PATH; p++) {
if (ISSLASH(*p) && len > 0) {
if (MKDIR(npath) == -1)
if (errno != EEXIST) return -1;
}
npath[len++] = *p;
}
return 0;
}
static char *strrpl(const char *str, char oldchar, char newchar) {
char *rpl = (char *)malloc(sizeof(char) * (1 + strlen(str)));
char *begin = rpl;
char c;
while((c = *str++)) {
if (c == oldchar) {
c = newchar;
}
*rpl++ = c;
}
*rpl = '\0';
return begin;
}
struct zip_entry_t {
int index;
const char *name;
mz_uint64 uncomp_size;
mz_uint64 comp_size;
mz_uint32 uncomp_crc32;
mz_uint64 offset;
mz_uint8 header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE];
mz_uint64 header_offset;
mz_uint16 method;
mz_zip_writer_add_state state;
tdefl_compressor comp;
};
struct zip_t {
mz_zip_archive archive;
mz_uint level;
struct zip_entry_t entry;
char mode;
};
struct zip_t *zip_open(const char *zipname, int level, char mode) {
struct zip_t *zip = NULL;
if (!zipname || strlen(zipname) < 1) {
// zip_t archive name is empty or NULL
goto cleanup;
}
if (level < 0) level = MZ_DEFAULT_LEVEL;
if ((level & 0xF) > MZ_UBER_COMPRESSION) {
// Wrong compression level
goto cleanup;
}
zip = (struct zip_t *)calloc((size_t)1, sizeof(struct zip_t));
if (!zip) goto cleanup;
zip->level = level;
zip->mode = mode;
switch (mode) {
case 'w':
// Create a new archive.
if (!mz_zip_writer_init_file(&(zip->archive), zipname, 0)) {
// Cannot initialize zip_archive writer
goto cleanup;
}
break;
case 'r':
case 'a':
if (!mz_zip_reader_init_file(
&(zip->archive), zipname,
level | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY)) {
// An archive file does not exist or cannot initialize
// zip_archive reader
goto cleanup;
}
if (mode == 'a' &&
!mz_zip_writer_init_from_reader(&(zip->archive), zipname)) {
mz_zip_reader_end(&(zip->archive));
goto cleanup;
}
break;
default:
goto cleanup;
}
return zip;
cleanup:
CLEANUP(zip);
return NULL;
}
void zip_close(struct zip_t *zip) {
if (zip) {
// Always finalize, even if adding failed for some reason, so we have a
// valid central directory.
mz_zip_writer_finalize_archive(&(zip->archive));
mz_zip_writer_end(&(zip->archive));
mz_zip_reader_end(&(zip->archive));
CLEANUP(zip);
}
}
int zip_entry_open(struct zip_t *zip, const char *entryname) {
char *locname = NULL;
size_t entrylen = 0;
mz_zip_archive *pzip = NULL;
mz_uint num_alignment_padding_bytes, level;
if (!zip || !entryname) {
return -1;
}
entrylen = strlen(entryname);
if (entrylen < 1) {
return -1;
}
pzip = &(zip->archive);
/*
.ZIP File Format Specification Version: 6.3.3
4.4.17.1 The name of the file, with optional relative path.
The path stored MUST not contain a drive or
device letter, or a leading slash. All slashes
MUST be forward slashes '/' as opposed to
backwards slashes '\' for compatibility with Amiga
and UNIX file systems etc. If input came from standard
input, there is no file name field.
*/
locname = strrpl(entryname, '\\', '/');
if (zip->mode == 'r') {
zip->entry.index = mz_zip_reader_locate_file(pzip, locname, NULL, 0);
CLEANUP(locname);
return (zip->entry.index < 0) ? -1 : 0;
}
zip->entry.index = zip->archive.m_total_files;
zip->entry.name = locname;
if (!zip->entry.name) {
// Cannot parse zip entry name
return -1;
}
zip->entry.comp_size = 0;
zip->entry.uncomp_size = 0;
zip->entry.uncomp_crc32 = MZ_CRC32_INIT;
zip->entry.offset = zip->archive.m_archive_size;
zip->entry.header_offset = zip->archive.m_archive_size;
memset(zip->entry.header, 0,
MZ_ZIP_LOCAL_DIR_HEADER_SIZE * sizeof(mz_uint8));
zip->entry.method = 0;
num_alignment_padding_bytes =
mz_zip_writer_compute_padding_needed_for_file_alignment(pzip);
if (!pzip->m_pState || (pzip->m_zip_mode != MZ_ZIP_MODE_WRITING)) {
// Wrong zip mode
return -1;
}
if (zip->level & MZ_ZIP_FLAG_COMPRESSED_DATA) {
// Wrong zip compression level
return -1;
}
// no zip64 support yet
if ((pzip->m_total_files == 0xFFFF) ||
((pzip->m_archive_size + num_alignment_padding_bytes +
MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE +
entrylen) > 0xFFFFFFFF)) {
// No zip64 support yet
return -1;
}
if (!mz_zip_writer_write_zeros(
pzip, zip->entry.offset,
num_alignment_padding_bytes + sizeof(zip->entry.header))) {
// Cannot memset zip entry header
return -1;
}
zip->entry.header_offset += num_alignment_padding_bytes;
if (pzip->m_file_offset_alignment) {
MZ_ASSERT((zip->entry.header_offset &
(pzip->m_file_offset_alignment - 1)) == 0);
}
zip->entry.offset +=
num_alignment_padding_bytes + sizeof(zip->entry.header);
if (pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.offset, zip->entry.name,
entrylen) != entrylen) {
// Cannot write data to zip entry
return -1;
}
zip->entry.offset += entrylen;
level = zip->level & 0xF;
if (level) {
zip->entry.state.m_pZip = pzip;
zip->entry.state.m_cur_archive_file_ofs = zip->entry.offset;
zip->entry.state.m_comp_size = 0;
if (tdefl_init(&(zip->entry.comp), mz_zip_writer_add_put_buf_callback,
&(zip->entry.state),
tdefl_create_comp_flags_from_zip_params(
level, -15, MZ_DEFAULT_STRATEGY)) !=
TDEFL_STATUS_OKAY) {
// Cannot initialize the zip compressor
return -1;
}
}
return 0;
}
int zip_entry_close(struct zip_t *zip) {
mz_zip_archive *pzip = NULL;
mz_uint level;
tdefl_status done;
mz_uint16 entrylen;
time_t t;
struct tm *tm;
mz_uint16 dos_time, dos_date;
int status = -1;
if (!zip) {
// zip_t handler is not initialized
return -1;
}
if (zip->mode == 'r') {
return 0;
}
pzip = &(zip->archive);
level = zip->level & 0xF;
if (level) {
done = tdefl_compress_buffer(&(zip->entry.comp), "", 0, TDEFL_FINISH);
if (done != TDEFL_STATUS_DONE && done != TDEFL_STATUS_OKAY) {
// Cannot flush compressed buffer
goto cleanup;
}
zip->entry.comp_size = zip->entry.state.m_comp_size;
zip->entry.offset = zip->entry.state.m_cur_archive_file_ofs;
zip->entry.method = MZ_DEFLATED;
}
entrylen = (mz_uint16)strlen(zip->entry.name);
t = time(NULL);
tm = localtime(&t);
dos_time = (mz_uint16)(((tm->tm_hour) << 11) + ((tm->tm_min) << 5) +
((tm->tm_sec) >> 1));
dos_date = (mz_uint16)(((tm->tm_year + 1900 - 1980) << 9) +
((tm->tm_mon + 1) << 5) + tm->tm_mday);
// no zip64 support yet
if ((zip->entry.comp_size > 0xFFFFFFFF) ||
(zip->entry.offset > 0xFFFFFFFF)) {
// No zip64 support, yet
goto cleanup;
}
if (!mz_zip_writer_create_local_dir_header(
pzip, zip->entry.header, entrylen, 0, zip->entry.uncomp_size,
zip->entry.comp_size, zip->entry.uncomp_crc32, zip->entry.method, 0,
dos_time, dos_date)) {
// Cannot create zip entry header
goto cleanup;
}
if (pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.header_offset,
zip->entry.header, sizeof(zip->entry.header)) !=
sizeof(zip->entry.header)) {
// Cannot write zip entry header
goto cleanup;
}
if (!mz_zip_writer_add_to_central_dir(
pzip, zip->entry.name, entrylen, NULL, 0, "", 0,
zip->entry.uncomp_size, zip->entry.comp_size,
zip->entry.uncomp_crc32, zip->entry.method, 0, dos_time, dos_date,
zip->entry.header_offset, 0)) {
// Cannot write to zip central dir
goto cleanup;
}
pzip->m_total_files++;
pzip->m_archive_size = zip->entry.offset;
status = 0;
cleanup:
CLEANUP(zip->entry.name);
return status;
}
int zip_entry_write(struct zip_t *zip, const void *buf, size_t bufsize) {
mz_uint level;
mz_zip_archive *pzip = NULL;
tdefl_status status;
if (!zip) {
// zip_t handler is not initialized
return -1;
}
pzip = &(zip->archive);
if (buf && bufsize > 0) {
zip->entry.uncomp_size += bufsize;
zip->entry.uncomp_crc32 = (mz_uint32)mz_crc32(
zip->entry.uncomp_crc32, (const mz_uint8 *)buf, bufsize);
level = zip->level & 0xF;
if (!level) {
if ((pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.offset, buf,
bufsize) != bufsize)) {
// Cannot write buffer
return -1;
}
zip->entry.offset += bufsize;
zip->entry.comp_size += bufsize;
} else {
status = tdefl_compress_buffer(&(zip->entry.comp), buf, bufsize,
TDEFL_NO_FLUSH);
if (status != TDEFL_STATUS_DONE && status != TDEFL_STATUS_OKAY) {
// Cannot compress buffer
return -1;
}
}
}
return 0;
}
int zip_entry_fwrite(struct zip_t *zip, const char *filename) {
int status = 0;
size_t n = 0;
FILE *stream = NULL;
mz_uint8 buf[MZ_ZIP_MAX_IO_BUF_SIZE] = {0};
if (!zip) {
// zip_t handler is not initialized
return -1;
}
stream = fopen(filename, "rb");
if (!stream) {
// Cannot open filename
return -1;
}
while ((n = fread(buf, sizeof(mz_uint8), MZ_ZIP_MAX_IO_BUF_SIZE, stream)) >
0) {
if (zip_entry_write(zip, buf, n) < 0) {
status = -1;
break;
}
}
fclose(stream);
return status;
}
int zip_entry_read(struct zip_t *zip, void **buf, size_t *bufsize) {
mz_zip_archive *pzip = NULL;
mz_uint idx;
if (!zip) {
// zip_t handler is not initialized
return -1;
}
if (zip->mode != 'r' || zip->entry.index < 0) {
// the entry is not found or we do not have read access
return -1;
}
pzip = &(zip->archive);
idx = (mz_uint)zip->entry.index;
if (mz_zip_reader_is_file_a_directory(pzip, idx)) {
// the entry is a directory
return -1;
}
*buf = mz_zip_reader_extract_to_heap(pzip, idx, bufsize, 0);
return (*buf) ? 0 : -1;
}
int zip_entry_fread(struct zip_t *zip, const char *filename) {
mz_zip_archive *pzip = NULL;
mz_uint idx;
if (!zip) {
// zip_t handler is not initialized
return -1;
}
if (zip->mode != 'r' || zip->entry.index < 0) {
// the entry is not found or we do not have read access
return -1;
}
pzip = &(zip->archive);
idx = (mz_uint)zip->entry.index;
if (mz_zip_reader_is_file_a_directory(pzip, idx)) {
// the entry is a directory
return -1;
}
return (mz_zip_reader_extract_to_file(pzip, idx, filename, 0)) ? 0 : -1;
}
int zip_entry_extract(struct zip_t *zip,
size_t (*on_extract)(void *arg, unsigned long long offset,
const void *buf, size_t bufsize),
void *arg) {
mz_zip_archive *pzip = NULL;
mz_uint idx;
if (!zip) {
// zip_t handler is not initialized
return -1;
}
if (zip->mode != 'r' || zip->entry.index < 0) {
// the entry is not found or we do not have read access
return -1;
}
pzip = &(zip->archive);
idx = (mz_uint)zip->entry.index;
return (mz_zip_reader_extract_to_callback(pzip, idx, on_extract, arg, 0))
? 0
: -1;
}
int zip_create(const char *zipname, const char *filenames[], size_t len) {
int status = 0;
size_t i;
mz_zip_archive zip_archive;
if (!zipname || strlen(zipname) < 1) {
// zip_t archive name is empty or NULL
return -1;
}
// Create a new archive.
if (!memset(&(zip_archive), 0, sizeof(zip_archive))) {
// Cannot memset zip archive
return -1;
}
if (!mz_zip_writer_init_file(&zip_archive, zipname, 0)) {
// Cannot initialize zip_archive writer
return -1;
}
for (i = 0; i < len; ++i) {
const char *name = filenames[i];
if (!name) {
status = -1;
break;
}
if (!mz_zip_writer_add_file(&zip_archive, basename(name), name, "", 0,
ZIP_DEFAULT_COMPRESSION_LEVEL)) {
// Cannot add file to zip_archive
status = -1;
break;
}
}
mz_zip_writer_finalize_archive(&zip_archive);
mz_zip_writer_end(&zip_archive);
return status;
}
int zip_extract(const char *zipname, const char *dir,
int (*on_extract)(const char *filename, void *arg), void *arg) {
int status = -1;
mz_uint i, n;
char path[MAX_PATH + 1] = {0};
mz_zip_archive zip_archive;
mz_zip_archive_file_stat info;
size_t dirlen = 0;
if (!memset(&(zip_archive), 0, sizeof(zip_archive))) {
// Cannot memset zip archive
return -1;
}
if (!zipname || !dir) {
// Cannot parse zip archive name
return -1;
}
dirlen = strlen(dir);
if (dirlen + 1 > MAX_PATH) {
return -1;
}
// Now try to open the archive.
if (!mz_zip_reader_init_file(&zip_archive, zipname, 0)) {
// Cannot initialize zip_archive reader
return -1;
}
strcpy(path, dir);
if (!ISSLASH(path[dirlen - 1])) {
#if defined _WIN32 || defined __WIN32__
path[dirlen] = '\\';
#else
path[dirlen] = '/';
#endif
++dirlen;
}
// Get and print information about each file in the archive.
n = mz_zip_reader_get_num_files(&zip_archive);
for (i = 0; i < n; ++i) {
if (!mz_zip_reader_file_stat(&zip_archive, i, &info)) {
// Cannot get information about zip archive;
goto out;
}
strncpy(&path[dirlen], info.m_filename, MAX_PATH - dirlen);
if (mkpath(path) < 0) {
// Cannot make a path
goto out;
}
if (!mz_zip_reader_is_file_a_directory(&zip_archive, i)) {
if (!mz_zip_reader_extract_to_file(&zip_archive, i, path, 0)) {
// Cannot extract zip archive to file
goto out;
}
}
if (on_extract) {
if (on_extract(path, arg) < 0) {
goto out;
}
}
}
status = 0;
out:
// Close the archive, freeing any resources it was using
if (!mz_zip_reader_end(&zip_archive)) {
// Cannot end zip reader
status = -1;
}
return status;
}

View File

@ -0,0 +1,193 @@
/*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#ifndef ZIP_H
#define ZIP_H
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef MAX_PATH
#define MAX_PATH 32767 /* # chars in a path name including NULL */
#endif
#define ZIP_DEFAULT_COMPRESSION_LEVEL 6
/*
This data structure is used throughout the library to represent zip archive
- forward declaration.
*/
struct zip_t;
/*
Opens zip archive with compression level using the given mode.
Args:
zipname: zip archive file name.
level: compression level (0-9 are the standard zlib-style levels).
mode: file access mode.
'r': opens a file for reading/extracting (the file must exists).
'w': creates an empty file for writing.
'a': appends to an existing archive.
Returns:
The zip archive handler or NULL on error
*/
extern struct zip_t *zip_open(const char *zipname, int level, char mode);
/*
Closes zip archive, releases resources - always finalize.
Args:
zip: zip archive handler.
*/
extern void zip_close(struct zip_t *zip);
/*
Opens a new entry for writing in a zip archive.
Args:
zip: zip archive handler.
entryname: an entry name in local dictionary.
Returns:
The return code - 0 on success, negative number (< 0) on error.
*/
extern int zip_entry_open(struct zip_t *zip, const char *entryname);
/*
Closes a zip entry, flushes buffer and releases resources.
Args:
zip: zip archive handler.
Returns:
The return code - 0 on success, negative number (< 0) on error.
*/
extern int zip_entry_close(struct zip_t *zip);
/*
Compresses an input buffer for the current zip entry.
Args:
zip: zip archive handler.
buf: input buffer.
bufsize: input buffer size (in bytes).
Returns:
The return code - 0 on success, negative number (< 0) on error.
*/
extern int zip_entry_write(struct zip_t *zip, const void *buf, size_t bufsize);
/*
Compresses a file for the current zip entry.
Args:
zip: zip archive handler.
filename: input file.
Returns:
The return code - 0 on success, negative number (< 0) on error.
*/
extern int zip_entry_fwrite(struct zip_t *zip, const char *filename);
/*
Extracts the current zip entry into output buffer.
The function allocates sufficient memory for a output buffer.
Args:
zip: zip archive handler.
buf: output buffer.
bufsize: output buffer size (in bytes).
Note:
- remember to release memory allocated for a output buffer.
- for large entries, please take a look at zip_entry_extract function.
Returns:
The return code - 0 on success, negative number (< 0) on error.
*/
extern int zip_entry_read(struct zip_t *zip, void **buf, size_t *bufsize);
/*
Extracts the current zip entry into output file.
Args:
zip: zip archive handler.
filename: output file.
Returns:
The return code - 0 on success, negative number (< 0) on error.
*/
extern int zip_entry_fread(struct zip_t *zip, const char *filename);
/*
Extract the current zip entry using a callback function (on_extract).
Args:
zip: zip archive handler.
on_extract: callback function.
arg: opaque pointer (optional argument,
which you can pass to the on_extract callback)
Returns:
The return code - 0 on success, negative number (< 0) on error.
*/
extern int zip_entry_extract(struct zip_t *zip,
size_t (*on_extract)(void *arg,
unsigned long long offset,
const void *data,
size_t size),
void *arg);
/*
Creates a new archive and puts files into a single zip archive.
Args:
zipname: zip archive file.
filenames: input files.
len: number of input files.
Returns:
The return code - 0 on success, negative number (< 0) on error.
*/
extern int zip_create(const char *zipname, const char *filenames[], size_t len);
/*
Extracts a zip archive file into directory.
If on_extract_entry is not NULL, the callback will be called after
successfully extracted each zip entry.
Returning a negative value from the callback will cause abort and return an
error. The last argument (void *arg) is optional, which you can use to pass
data to the on_extract_entry callback.
Args:
zipname: zip archive file.
dir: output directory.
on_extract_entry: on extract callback.
arg: opaque pointer.
Returns:
The return code - 0 on success, negative number (< 0) on error.
*/
extern int zip_extract(const char *zipname, const char *dir,
int (*on_extract_entry)(const char *filename, void *arg),
void *arg);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 2.8)
# test
include_directories(../src)
add_executable(test.exe test.c ../src/zip.c)
add_test(NAME test COMMAND test.exe)

View File

@ -0,0 +1,105 @@
#include <zip.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ZIPNAME "test.zip"
#define TESTDATA1 "Some test data 1...\0"
#define TESTDATA2 "Some test data 2...\0"
static void test_write(void) {
struct zip_t *zip = zip_open(ZIPNAME, ZIP_DEFAULT_COMPRESSION_LEVEL, 'w');
assert(zip != NULL);
assert(0 == zip_entry_open(zip, "test/test-1.txt"));
assert(0 == zip_entry_write(zip, TESTDATA1, strlen(TESTDATA1)));
assert(0 == zip_entry_close(zip));
zip_close(zip);
}
static void test_append(void) {
struct zip_t *zip = zip_open(ZIPNAME, ZIP_DEFAULT_COMPRESSION_LEVEL, 'a');
assert(zip != NULL);
assert(0 == zip_entry_open(zip, "test\\test-2.txt"));
assert(0 == zip_entry_write(zip, TESTDATA2, strlen(TESTDATA2)));
assert(0 == zip_entry_close(zip));
zip_close(zip);
}
static void test_read(void) {
char *buf = NULL;
size_t bufsize;
struct zip_t *zip = zip_open(ZIPNAME, 0, 'r');
assert(zip != NULL);
assert(0 == zip_entry_open(zip, "test\\test-1.txt"));
assert(0 == zip_entry_read(zip, (void **)&buf, &bufsize));
assert(bufsize == strlen(TESTDATA1));
assert(0 == strncmp(buf, TESTDATA1, bufsize));
assert(0 == zip_entry_close(zip));
free(buf);
buf = NULL;
bufsize = 0;
assert(0 == zip_entry_open(zip, "test/test-2.txt"));
assert(0 == zip_entry_read(zip, (void **)&buf, &bufsize));
assert(bufsize == strlen(TESTDATA2));
assert(0 == strncmp(buf, TESTDATA2, bufsize));
assert(0 == zip_entry_close(zip));
free(buf);
buf = NULL;
bufsize = 0;
zip_close(zip);
}
struct buffer_t {
char *data;
size_t size;
};
static size_t on_extract(void *arg, unsigned long long offset, const void *data,
size_t size) {
struct buffer_t *buf = (struct buffer_t *)arg;
buf->data = realloc(buf->data, buf->size + size + 1);
assert(NULL != buf->data);
memcpy(&(buf->data[buf->size]), data, size);
buf->size += size;
buf->data[buf->size] = 0;
return size;
}
static void test_extract(void) {
struct buffer_t buf = {0};
struct zip_t *zip = zip_open(ZIPNAME, 0, 'r');
assert(zip != NULL);
assert(0 == zip_entry_open(zip, "test/test-1.txt"));
assert(0 == zip_entry_extract(zip, on_extract, &buf));
assert(buf.size == strlen(TESTDATA1));
assert(0 == strncmp(buf.data, TESTDATA1, buf.size));
assert(0 == zip_entry_close(zip));
free(buf.data);
buf.data = NULL;
buf.size = 0;
zip_close(zip);
}
int main(int argc, char *argv[]) {
test_write();
test_append();
test_read();
test_extract();
return remove(ZIPNAME);
}

BIN
contrib/zip/zip.png 100644

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

View File

@ -56,9 +56,20 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "types.h"
#ifdef _WIN32
# include <direct.h>
# include <stdlib.h>
# include <stdio.h>
#else
# include <sys/stat.h>
# include <sys/types.h>
# include <unistd.h>
#endif // _WIN32
#include <vector>
namespace Assimp {
class IOStream;
// ---------------------------------------------------------------------------
@ -198,20 +209,35 @@ public:
*/
virtual bool PopDirectory();
// -------------------------------------------------------------------
/** @brief CReates an new directory at the given path.
* @param path [in] The path to create.
* @return True, when a directory was created. False if the directory
* cannot be created.
*/
virtual bool CreateDirectory( const std::string &path );
// -------------------------------------------------------------------
/** @brief Will change the current directory to the given path.
* @param path [in] The path to change to.
* @return True, when the directory has changed successfully.
*/
virtual bool ChangeDirectory( const std::string &path );
private:
std::vector<std::string> m_pathStack;
};
// ----------------------------------------------------------------------------
AI_FORCE_INLINE IOSystem::IOSystem() :
m_pathStack()
{
AI_FORCE_INLINE
IOSystem::IOSystem()
: m_pathStack() {
// empty
}
// ----------------------------------------------------------------------------
AI_FORCE_INLINE IOSystem::~IOSystem()
{
AI_FORCE_INLINE
IOSystem::~IOSystem() {
// empty
}
@ -222,9 +248,8 @@ AI_FORCE_INLINE IOSystem::~IOSystem()
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
AI_FORCE_INLINE IOStream* IOSystem::Open(const std::string& pFile,
const std::string& pMode)
{
AI_FORCE_INLINE
IOStream* IOSystem::Open(const std::string& pFile, const std::string& pMode) {
// NOTE:
// For compatibility, interface was changed to const char* to
// avoid crashes between binary incompatible STL versions
@ -232,8 +257,8 @@ AI_FORCE_INLINE IOStream* IOSystem::Open(const std::string& pFile,
}
// ----------------------------------------------------------------------------
AI_FORCE_INLINE bool IOSystem::Exists( const std::string& pFile) const
{
AI_FORCE_INLINE
bool IOSystem::Exists( const std::string& pFile) const {
// NOTE:
// For compatibility, interface was changed to const char* to
// avoid crashes between binary incompatible STL versions
@ -241,9 +266,8 @@ AI_FORCE_INLINE bool IOSystem::Exists( const std::string& pFile) const
}
// ----------------------------------------------------------------------------
inline bool IOSystem::ComparePaths (const std::string& one,
const std::string& second) const
{
AI_FORCE_INLINE
bool IOSystem::ComparePaths (const std::string& one, const std::string& second) const {
// NOTE:
// For compatibility, interface was changed to const char* to
// avoid crashes between binary incompatible STL versions
@ -251,7 +275,8 @@ inline bool IOSystem::ComparePaths (const std::string& one,
}
// ----------------------------------------------------------------------------
inline bool IOSystem::PushDirectory( const std::string &path ) {
AI_FORCE_INLINE
bool IOSystem::PushDirectory( const std::string &path ) {
if ( path.empty() ) {
return false;
}
@ -262,7 +287,8 @@ inline bool IOSystem::PushDirectory( const std::string &path ) {
}
// ----------------------------------------------------------------------------
inline const std::string &IOSystem::CurrentDirectory() const {
AI_FORCE_INLINE
const std::string &IOSystem::CurrentDirectory() const {
if ( m_pathStack.empty() ) {
static const std::string Dummy("");
return Dummy;
@ -271,12 +297,14 @@ inline const std::string &IOSystem::CurrentDirectory() const {
}
// ----------------------------------------------------------------------------
inline size_t IOSystem::StackSize() const {
AI_FORCE_INLINE
size_t IOSystem::StackSize() const {
return m_pathStack.size();
}
// ----------------------------------------------------------------------------
inline bool IOSystem::PopDirectory() {
AI_FORCE_INLINE
bool IOSystem::PopDirectory() {
if ( m_pathStack.empty() ) {
return false;
}
@ -287,6 +315,32 @@ inline bool IOSystem::PopDirectory() {
}
// ----------------------------------------------------------------------------
AI_FORCE_INLINE
bool IOSystem::CreateDirectory( const std::string &path ) {
if ( path.empty() ) {
return false;
}
#ifdef _WIN32
return 0 != ::_mkdir( path.c_str() );
#else
return 0 != ::mkdir( path.c_str(), 0777 );
#endif // _WIN32
}
// ----------------------------------------------------------------------------
AI_FORCE_INLINE
bool IOSystem::ChangeDirectory( const std::string &path ) {
if ( path.empty() ) {
return false;
}
#ifdef _WIN32
return 0 != ::_chdir( path.c_str() );
#else
return 0 != ::chdir( path.c_str() );
#endif // _WIN32
}
} //!ns Assimp

View File

@ -43,9 +43,12 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "AbstractImportExportBase.h"
#include <assimp/Importer.hpp>
#include <assimp/Exporter.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include "D3MFExporter.h"
class utD3MFImporterExporter : public AbstractImportExportBase {
public:
virtual bool importerTest() {
@ -59,8 +62,27 @@ public:
return ( nullptr != scene );
}
#ifndef ASSIMP_BUILD_NO_EXPORT
virtual bool exporterTest() {
Assimp::Importer importer;
const aiScene *scene = importer.ReadFile( ASSIMP_TEST_MODELS_DIR "/3MF/box.3mf", 0 );
Assimp::Exporter exporter;
return AI_SUCCESS == exporter.Export( scene, "3mf", "test.3mf" );
}
#endif // ASSIMP_BUILD_NO_EXPORT
};
TEST_F(utD3MFImporterExporter, import3MFFromFileTest) {
EXPECT_TRUE(importerTest());
}
#ifndef ASSIMP_BUILD_NO_EXPORT
TEST_F( utD3MFImporterExporter, export3MFtoMemTest ) {
EXPECT_TRUE( exporterTest() );
}
#endif // ASSIMP_BUILD_NO_EXPORT