[mapguide-commits] r9440 - sandbox/jng/deprecate_dwf/Server/src/Services/Resource

svn_mapguide at osgeo.org svn_mapguide at osgeo.org
Sat Jan 5 09:36:37 PST 2019


Author: jng
Date: 2019-01-05 09:36:37 -0800 (Sat, 05 Jan 2019)
New Revision: 9440

Modified:
   sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ServerResourceService.vcxproj
   sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ServerResourceService.vcxproj.filters
   sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ServerResourceServiceBuild.cpp
   sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ZipFileReader.cpp
   sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ZipFileReader.h
   sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ZipFileWriter.cpp
   sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ZipFileWriter.h
Log:
Initial implementation of minizip-based MGP reading and writing.

Modified: sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ServerResourceService.vcxproj
===================================================================
--- sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ServerResourceService.vcxproj	2019-01-05 16:01:38 UTC (rev 9439)
+++ sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ServerResourceService.vcxproj	2019-01-05 17:36:37 UTC (rev 9440)
@@ -663,12 +663,6 @@
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
     </ClCompile>
-    <ClCompile Include="ZipFileHandler.cpp">
-      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
-      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
-      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
-      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
-    </ClCompile>
     <ClCompile Include="ZipFileReader.cpp">
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
@@ -761,7 +755,6 @@
     <ClInclude Include="TagInfo.h" />
     <ClInclude Include="TagManager.h" />
     <ClInclude Include="XmlSchemaResolver.h" />
-    <ClInclude Include="ZipFileHandler.h" />
     <ClInclude Include="ZipFileReader.h" />
     <ClInclude Include="ZipFileWriter.h" />
   </ItemGroup>

Modified: sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ServerResourceService.vcxproj.filters
===================================================================
--- sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ServerResourceService.vcxproj.filters	2019-01-05 16:01:38 UTC (rev 9439)
+++ sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ServerResourceService.vcxproj.filters	2019-01-05 17:36:37 UTC (rev 9440)
@@ -105,7 +105,6 @@
     <ClCompile Include="ApplicationRepository.cpp" />
     <ClCompile Include="ApplicationRepositoryManager.cpp" />
     <ClCompile Include="ApplicationResourceContentManager.cpp" />
-    <ClCompile Include="ByteSourceDwfInputStreamImpl.cpp" />
     <ClCompile Include="Database.cpp" />
     <ClCompile Include="DataBindingInfo.cpp" />
     <ClCompile Include="DbEnvironment.cpp" />
@@ -147,7 +146,6 @@
     <ClCompile Include="TagInfo.cpp" />
     <ClCompile Include="TagManager.cpp" />
     <ClCompile Include="XmlSchemaResolver.cpp" />
-    <ClCompile Include="ZipFileHandler.cpp" />
     <ClCompile Include="ZipFileReader.cpp" />
     <ClCompile Include="ZipFileWriter.cpp" />
     <ClCompile Include="OpEnumerateParentTileSetDefinitions.cpp">
@@ -254,7 +252,6 @@
     <ClInclude Include="ApplicationRepository.h" />
     <ClInclude Include="ApplicationRepositoryManager.h" />
     <ClInclude Include="ApplicationResourceContentManager.h" />
-    <ClInclude Include="ByteSourceDwfInputStreamImpl.h" />
     <ClInclude Include="Database.h" />
     <ClInclude Include="DataBindingInfo.h" />
     <ClInclude Include="DbEnvironment.h" />
@@ -297,7 +294,6 @@
     <ClInclude Include="TagInfo.h" />
     <ClInclude Include="TagManager.h" />
     <ClInclude Include="XmlSchemaResolver.h" />
-    <ClInclude Include="ZipFileHandler.h" />
     <ClInclude Include="ZipFileReader.h" />
     <ClInclude Include="ZipFileWriter.h" />
     <ClInclude Include="OpEnumerateParentTileSetDefinitions.h">

Modified: sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ServerResourceServiceBuild.cpp
===================================================================
--- sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ServerResourceServiceBuild.cpp	2019-01-05 16:01:38 UTC (rev 9439)
+++ sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ServerResourceServiceBuild.cpp	2019-01-05 17:36:37 UTC (rev 9440)
@@ -96,6 +96,5 @@
 #include "TagInfo.cpp"
 #include "TagManager.cpp"
 #include "XmlSchemaResolver.cpp"
-#include "ZipFileHandler.cpp"
 #include "ZipFileReader.cpp"
 #include "ZipFileWriter.cpp"

Modified: sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ZipFileReader.cpp
===================================================================
--- sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ZipFileReader.cpp	2019-01-05 16:01:38 UTC (rev 9439)
+++ sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ZipFileReader.cpp	2019-01-05 17:36:37 UTC (rev 9440)
@@ -18,13 +18,62 @@
 #include "ResourceServiceDefs.h"
 #include "ZipFileReader.h"
 
+//Bulk of this code copied from miniunz.c reference implementation
+
+class MgByteSourceUnzipImpl : public ByteSourceImpl
+{
+public:
+    MgByteSourceUnzipImpl(unz_file_info currentFileInfo, unzFile unzip) 
+        : m_fi(currentFileInfo), m_unzip(unzip), m_read(0)
+    { 
+        int err = unzOpenCurrentFile(m_unzip);
+        if (err != UNZ_OK)
+        {
+            //throw;
+        }
+    }
+    virtual ~MgByteSourceUnzipImpl() 
+    { 
+        unzCloseCurrentFile(m_unzip);
+    }
+    INT64 GetLength() { return m_fi.uncompressed_size - m_read; }
+    bool IsRewindable() { return false;  }
+    void Rewind() { }
+    INT32 Read(BYTE_ARRAY_OUT buffer, INT32 length)
+    {
+        INT32 err = unzReadCurrentFile(m_unzip, buffer, length);
+        m_read += err;
+        return err;
+    }
+
+private:
+    unz_file_info m_fi;
+    unzFile m_unzip;
+    INT32 m_read;
+
+    // Unimplemented Methods
+
+    MgByteSourceUnzipImpl();
+    MgByteSourceUnzipImpl(const MgByteSourceUnzipImpl&);
+    MgByteSourceUnzipImpl& operator=(
+        const MgByteSourceUnzipImpl&);
+};
+
 ///////////////////////////////////////////////////////////////////////////////
 /// \brief
 /// Constructs the object.
 ///
-MgZipFileReader::MgZipFileReader(CREFSTRING filePath) :
-    MgZipFileHandler(filePath, MgZipFileMode::Read)
+MgZipFileReader::MgZipFileReader(CREFSTRING filePath) 
+    : m_unzip(NULL)
 {
+    std::string mbFilePath = MgUtil::WideCharToMultiByte(filePath);
+    m_unzip = unzOpen(mbFilePath.c_str());
+    if (!m_unzip)
+    {
+        MgStringCollection arguments;
+        arguments.Add(filePath);
+        throw new MgInvalidDwfPackageException(L"MgZipFileReader.MgZipFileReader", __LINE__, __WFILE__, &arguments, L"", NULL);
+    }
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -33,6 +82,10 @@
 ///
 MgZipFileReader::~MgZipFileReader()
 {
+    if (m_unzip)
+    {
+        unzCloseCurrentFile(m_unzip);
+    }
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -43,21 +96,36 @@
 {
     ACE_ASSERT(!filePath.empty());
     Ptr<MgByteSource> byteSource;
-    /*
+
+    char* mbFilePath = MgUtil::WideCharToMultiByte(filePath.c_str());
+
     MG_RESOURCE_SERVICE_TRY()
 
-    // Note that the byteSource object owns the byteSourceImpl object whose
-    // destructor will de-allocate the inputStream object using the macro
-    // DWFCORE_FREE_OBJECT.
+    //Set pointer to location of specified file
+    if (unzLocateFile(m_unzip, mbFilePath, 0 /* Let platform decide case-sensitivity */) != UNZ_OK)
+    {
+        MgStringCollection arguments;
+        arguments.Add(filePath);
+        throw new MgInvalidDwfPackageException(L"MgZipFileReader.MgZipFileReader", __LINE__, __WFILE__, &arguments, L"", NULL);
+    }
 
-    DWFString archivedFile(filePath.c_str());
-    DWFInputStream* inputStream = m_zipFileDescriptor->unzip(archivedFile);
+    int err = UNZ_OK;
+    unz_file_info file_info;
+    err = unzGetCurrentFileInfo(m_unzip, &file_info, mbFilePath, strlen(mbFilePath), NULL, 0, NULL, 0);
+    if (err != UNZ_OK)
+    {
+        MgStringCollection arguments;
+        arguments.Add(filePath);
+        throw new MgInvalidDwfPackageException(L"MgZipFileReader.MgZipFileReader", __LINE__, __WFILE__, &arguments, L"", NULL);
+    }
 
-    MgByteSourceDwfInputStreamImpl* byteSourceImpl =
-        new MgByteSourceDwfInputStreamImpl(inputStream);
-    byteSource = new MgByteSource(byteSourceImpl);
+    MgByteSourceUnzipImpl* impl = new MgByteSourceUnzipImpl(file_info, m_unzip);
+    byteSource = new MgByteSource(impl);
 
-    MG_RESOURCE_SERVICE_CATCH_AND_THROW(L"MgZipFileReader.ExtractArchive")
-    */
+    MG_RESOURCE_SERVICE_CATCH(L"MgZipFileReader.ExtractArchive")
+
+        delete[] mbFilePath;
+
+    MG_RESOURCE_SERVICE_THROW()
     return byteSource.Detach();
 }

Modified: sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ZipFileReader.h
===================================================================
--- sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ZipFileReader.h	2019-01-05 16:01:38 UTC (rev 9439)
+++ sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ZipFileReader.h	2019-01-05 17:36:37 UTC (rev 9440)
@@ -18,9 +18,9 @@
 #ifndef MGZIPFILEREADER_H_
 #define MGZIPFILEREADER_H_
 
-#include "ZipFileHandler.h"
+#include "unzip.h"
 
-class MgZipFileReader : public MgZipFileHandler
+class MgZipFileReader
 {
 /// Constructors/Destructor
 
@@ -46,7 +46,7 @@
 /// Data Members
 
 private:
-
+    unzFile m_unzip;
 };
 
 /// Inline Methods

Modified: sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ZipFileWriter.cpp
===================================================================
--- sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ZipFileWriter.cpp	2019-01-05 16:01:38 UTC (rev 9439)
+++ sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ZipFileWriter.cpp	2019-01-05 17:36:37 UTC (rev 9440)
@@ -18,13 +18,82 @@
 #include "ResourceServiceDefs.h"
 #include "ZipFileWriter.h"
 
+//Bulk of this code copied from minizip.c reference implementation
+
+#ifdef _WIN32
+uLong filetime(const wchar_t* f /* name of file to get info on */, tm_zip* tmzip /* return value: access, modific. and creation times */, uLong* dt /* dostime */)
+{
+    int ret = 0;
+    {
+        FILETIME ftLocal;
+        HANDLE hFind;
+        WIN32_FIND_DATA  ff32;
+
+        hFind = FindFirstFile(f, &ff32);
+        if (hFind != INVALID_HANDLE_VALUE)
+        {
+            FileTimeToLocalFileTime(&(ff32.ftLastWriteTime), &ftLocal);
+            FileTimeToDosDateTime(&ftLocal, ((LPWORD)dt) + 1, ((LPWORD)dt) + 0);
+            FindClose(hFind);
+            ret = 1;
+        }
+    }
+    return ret;
+}
+#else
+uLong filetime(char* f /* name of file to get info on */, tm_zip* tmzip /* return value: access, modific. and creation times */, uLong* dt /* dostime */)
+{
+    int ret = 0;
+    struct stat s;        /* results of stat() */
+    struct tm* filedate;
+    time_t tm_t = 0;
+
+    if (strcmp(f, "-") != 0)
+    {
+        char name[MAXFILENAME + 1];
+        int len = strlen(f);
+        if (len > MAXFILENAME)
+            len = MAXFILENAME;
+
+        strncpy(name, f, MAXFILENAME - 1);
+        /* strncpy doesnt append the trailing NULL, of the string is too long. */
+        name[MAXFILENAME] = '\0';
+
+        if (name[len - 1] == '/')
+            name[len - 1] = '\0';
+        /* not all systems allow stat'ing a file with / appended */
+        if (stat(name, &s) == 0)
+        {
+            tm_t = s.st_mtime;
+            ret = 1;
+        }
+    }
+    filedate = localtime(&tm_t);
+
+    tmzip->tm_sec = filedate->tm_sec;
+    tmzip->tm_min = filedate->tm_min;
+    tmzip->tm_hour = filedate->tm_hour;
+    tmzip->tm_mday = filedate->tm_mday;
+    tmzip->tm_mon = filedate->tm_mon;
+    tmzip->tm_year = filedate->tm_year;
+
+    return ret;
+}
+#endif
+
 ///////////////////////////////////////////////////////////////////////////////
 /// \brief
 /// Constructs the object.
 ///
-MgZipFileWriter::MgZipFileWriter(CREFSTRING filePath) :
-    MgZipFileHandler(filePath, MgZipFileMode::Write)
+MgZipFileWriter::MgZipFileWriter(CREFSTRING filePath)
+    : m_zip(NULL)
 {
+    std::string mbFilePath = MgUtil::WideCharToMultiByte(filePath);
+    m_zip = zipOpen(mbFilePath.c_str(), 0);
+    if (!m_zip)
+    {
+        //throw
+    }
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -42,6 +111,69 @@
 void MgZipFileWriter::AddArchive(CREFSTRING filePath, MgByteReader* byteReader)
 {
     ACE_ASSERT(!filePath.empty());
+
+    char* mbFilePath = MgUtil::WideCharToMultiByte(filePath.c_str());
+    MG_RESOURCE_SERVICE_TRY()
+
+    CHECKARGUMENTNULL(byteReader, L"MgZipFileWriter.AddArchive");
+
+    int err = UNZ_OK;
+
+    zip_fileinfo zi;
+    unsigned long crcFile = 0;
+    const char* password = NULL;
+    int opt_compress_level = Z_DEFAULT_COMPRESSION;
+
+    zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
+        zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
+    zi.dosDate = 0;
+    zi.internal_fa = 0;
+    zi.external_fa = 0;
+
+#ifdef _WIN32
+    filetime(filePath.c_str(), &zi.tmz_date, &zi.dosDate);
+#else
+    filetime(mbFilePath, &zi.tmz_date, &zi.dosDate);
+#endif
+
+    err = zipOpenNewFileInZip3(m_zip, mbFilePath, &zi,
+        NULL, 0, NULL, 0, NULL /* comment*/,
+        (opt_compress_level != 0) ? Z_DEFLATED : 0,
+        opt_compress_level, 0,
+        /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */
+        -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
+        password, crcFile);
+
+    if (err != ZIP_OK)
+    {
+        //throw
+    }
+
+    const INT32 length = 4096;
+    BYTE buffer[length] = { 0 };
+    INT32 numBytes = 0;
+    do
+    {
+        numBytes = byteReader->Read(buffer, length);
+        err = zipWriteInFileInZip(m_zip, buffer, numBytes);
+        if (err < 0)
+        {
+            //throw
+        }
+    } while ((err == ZIP_OK) && (numBytes > 0));
+
+    err = zipCloseFileInZip(m_zip);
+    if (err != ZIP_OK)
+    {
+        //throw
+    }
+
+    MG_RESOURCE_SERVICE_CATCH(L"MgZipFileWriter.AddArchive")
+
+    delete[] mbFilePath;
+
+    MG_RESOURCE_SERVICE_THROW()
+
     /*
     DWFOutputStream* outputStream = NULL;
 

Modified: sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ZipFileWriter.h
===================================================================
--- sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ZipFileWriter.h	2019-01-05 16:01:38 UTC (rev 9439)
+++ sandbox/jng/deprecate_dwf/Server/src/Services/Resource/ZipFileWriter.h	2019-01-05 17:36:37 UTC (rev 9440)
@@ -18,9 +18,9 @@
 #ifndef MGZIPFILEWRITER_H_
 #define MGZIPFILEWRITER_H_
 
-#include "ZipFileHandler.h"
+#include "zip.h"
 
-class MgZipFileWriter : public MgZipFileHandler
+class MgZipFileWriter
 {
 /// Constructors/Destructor
 
@@ -47,7 +47,7 @@
 /// Data Members
 
 private:
-
+    zipFile m_zip;
 };
 
 /// Inline Methods



More information about the mapguide-commits mailing list