~ubuntu-branches/ubuntu/wily/opencollada/wily-proposed

« back to all changes in this revision

Viewing changes to G3DWarehouseBrowser/src/G3DFileUnzipper.cpp

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2015-05-14 17:23:27 UTC
  • Revision ID: package-import@ubuntu.com-20150514172327-f862u8envms01fra
Tags: upstream-0.1.0~20140703.ddf8f47+dfsg1
ImportĀ upstreamĀ versionĀ 0.1.0~20140703.ddf8f47+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 2009 NetAllied Systems GmbH
 
3
 
 
4
          This file is part of G3DWarehouseBrowser.
 
5
 
 
6
    Licensed under the MIT Open Source License, 
 
7
    for details please see LICENSE file or the website
 
8
    http://www.opensource.org/licenses/mit-license.php
 
9
*/
 
10
 
 
11
#include "G3DStableHeaders.h"
 
12
#include "G3DFileUnzipper.h"
 
13
 
 
14
#include <SYS\Stat.h>
 
15
#include <direct.h>
 
16
 
 
17
#include "zzip/zzip.h"
 
18
 
 
19
#include "COLLADABUStringUtils.h"
 
20
 
 
21
 
 
22
 
 
23
namespace G3D
 
24
{
 
25
 
 
26
        const char IMAGES_DIRECTORY[] = "images";
 
27
        const char MODEL_PATH[] = "models/model.dae";
 
28
        const char WAREHOUSE_MODEL_PATH[] = "models/warehouse_model.dae";
 
29
        const char MODEL_DIR[] = "models";
 
30
 
 
31
        const String SLASH("/");
 
32
        const String BACKSLASH("\\");
 
33
 
 
34
 
 
35
 
 
36
        String unzipFile_( ZZIP_DIR * dir, const ZZIP_DIRENT& d, const String& extractPath, String& errorString )
 
37
        {
 
38
                ZZIP_FILE* input = zzip_file_open (dir, d.d_name, O_RDONLY);
 
39
                if ( !input)
 
40
                {  
 
41
                        errorString = zzip_strerror_of(dir);
 
42
                        return String();
 
43
                }
 
44
                String filePath(d.d_name);
 
45
                COLLADABU::StringUtils::stringFindAndReplace(filePath, SLASH, BACKSLASH);
 
46
                String extractedFilePath = extractPath + filePath;
 
47
                FILE* outFile;
 
48
                errno_t  error = fopen_s( &outFile, extractedFilePath.c_str(), "wb"  );
 
49
                if (error)
 
50
                {
 
51
                        errorString = String("Could not open output file") + d.d_name;
 
52
                        perror(d.d_name);
 
53
                        zzip_file_close (input);
 
54
                        return String();
 
55
                }
 
56
 
 
57
                { 
 
58
                        char buf[17]; zzip_ssize_t n;
 
59
                        /* read chunks of 16 bytes into buf */
 
60
                        while (0 < (n = zzip_read (input, buf, 16)))
 
61
                        {
 
62
                                fwrite (buf, 1, n, outFile);
 
63
                        }
 
64
 
 
65
                        if (n == -1)
 
66
                                perror (d.d_name);
 
67
                }
 
68
                fclose (outFile);
 
69
                zzip_file_close (input);
 
70
 
 
71
                return extractedFilePath;
 
72
        }
 
73
 
 
74
 
 
75
 
 
76
        //--------------------------------------------------------------------
 
77
        FileUnzipper::FileUnzipper( const String& zipFile, const String& extractPath )
 
78
        {
 
79
                mSucceeded = unzipFile(zipFile, extractPath);
 
80
        }
 
81
 
 
82
        //--------------------------------------------------------------------
 
83
        FileUnzipper::~FileUnzipper()
 
84
        {
 
85
        }
 
86
 
 
87
        //--------------------------------------------------------------------
 
88
        bool FileUnzipper::unzipFile( const String& zipFile, const String& extractPath )
 
89
        {
 
90
 
 
91
                ZZIP_DIR * dir;
 
92
                ZZIP_DIRENT d;
 
93
                zzip_error_t error;
 
94
 
 
95
                dir = zzip_dir_open(zipFile.c_str(), &error);
 
96
                if (! dir)
 
97
                {
 
98
                        mLastErrorString = zzip_strerror(error);
 
99
                        return false;
 
100
                }
 
101
 
 
102
                /* read each dir entry and show one line of info per file */
 
103
                while (zzip_dir_read (dir, &d))
 
104
                {
 
105
 
 
106
                        if ( (strcmp(d.d_name, MODEL_PATH) == 0) || (strcmp(d.d_name, WAREHOUSE_MODEL_PATH) == 0) )
 
107
                        {
 
108
                                _mkdir( (extractPath+MODEL_DIR).c_str() );
 
109
                                mDAEFilePath = unzipFile_(dir, d, extractPath, mLastErrorString);
 
110
                                continue;
 
111
                        }
 
112
 
 
113
 
 
114
                        String filePath(d.d_name);
 
115
                        size_t separatorPos =  filePath.find_first_of('/');
 
116
                        String filePathDir(filePath, 0, separatorPos);
 
117
 
 
118
                        if ( filePathDir == IMAGES_DIRECTORY )
 
119
                        {
 
120
                                _mkdir( (extractPath+IMAGES_DIRECTORY).c_str() );
 
121
                                unzipFile_(dir, d, extractPath, mLastErrorString);
 
122
                                continue;
 
123
                        }
 
124
 
 
125
                }
 
126
                return true;
 
127
        }
 
128
 
 
129
        /*
 
130
        bool FileUnzipper::unzipDirectory( ZZIP_DIR * dir, ZZIP_DIRENT d, const String& extractPath )
 
131
        {
 
132
 
 
133
        }
 
134
 
 
135
        */
 
136
 
 
137
 
 
138
 
 
139
 
 
140
}
 
 
b'\\ No newline at end of file'