This commit is contained in:
2025-05-13 02:46:21 +03:00
parent 143e16692e
commit 86914b7fcc
610 changed files with 92343 additions and 2 deletions
+41
View File
@@ -0,0 +1,41 @@
#include "IZipFile.h"
#include "IZipFileContentProvider.h"
namespace LifeCore
{
IZipFile::IZipFile()
{
isOpen = false;
}
IDataStreamPtr IZipFile::OpenWrite(int mode)
{
UNREFERENCED_PARAMETER(mode);
g_stream = new IZipStream(((IZipFileContentProvider*)this->Provider)->g_sourceFile, EntryName, FA_WRITE, ((IZipFileContentProvider*)this->Provider)->g_rootFolder);
return IDataStreamPtr(g_stream);
}
IDataStreamPtr IZipFile::OpenReadWrite(int mode)
{
UNREFERENCED_PARAMETER(mode);
throw;
//return IDataStreamPtr(0);
//throw new NotImplementedException();
}
IDataStreamPtr IZipFile::OpenRead()
{
IZipFileContentProvider* prov = (IZipFileContentProvider*)this->Provider;
prov->g_sourceFile = unzOpen(prov->g_rootFolder.c_str());
g_stream = new IZipStream(prov->g_sourceFile, EntryName, FA_READ, NULL);
return IDataStreamPtr(g_stream);
//g_stream = new IZipStream((this->Provider as IZipFileContentProvider).GetSource(), EntryName, FA_READ);
//return g_stream;
}
void IZipFile::Close()
{
if(g_stream)
{
g_stream->Close();
delete g_stream;
}
}
}
+27
View File
@@ -0,0 +1,27 @@
#ifndef IZIPFILE_H__
#define IZIPFILE_H__
//#include "IDataStream.h"
#include "../../LString.h"
#include "../IFile.h"
#include "IZipStream.h"
namespace LifeCore
{
class IZipFile : public IFile
{
public:
IZipFile();
~IZipFile(){}
String EntryName;
IDataStreamPtr OpenWrite(int mode);
IDataStreamPtr OpenReadWrite(int mode);
IDataStreamPtr OpenRead();
void Close();
private:
int offsetIndex;
IZipStream* g_stream;
};
}
#endif
@@ -0,0 +1,83 @@
#include "IZipFileContentProvider.h"
namespace LifeCore
{
static const String zipPathSplitter(_text("/"));
IZipFileContentProvider::IZipFileContentProvider(const String& zipFile)
{
IZipFolder* folder = new IZipFolder();
g_rootFolder = zipFile;
folder->Provider = this;
folder->Name = GetFileNameWithoutExtention(zipFile);
folder->EntryName = _text("");
g_fs = new IFileSystem(folder);
g_sourceFile = unzOpen(zipFile.c_str());
unzGetGlobalInfo(g_sourceFile,&globalInfo);
g_fs->GetRootFolder();
BuildTree();
unzClose(g_sourceFile);
}
void IZipFileContentProvider::BuildTree()
{
unzGoToFirstFile(g_sourceFile);
for (unsigned int i = 0; i < globalInfo.number_entry; i++)
{
tchar fileName[256+1] = {0};
unz_file_info fileInfo;
unzGetCurrentFileInfo(g_sourceFile,&fileInfo,fileName,256,NULL,0,NULL, 0);
if (fileInfo.size_filename >0 && fileName[fileInfo.size_filename-1] == '/')
{
/* string path = entry.Name;
path = path.Replace('/', '\\');
IZipFolder folder = BuildDirectory(path);*/
}
else//is file
{
String sd = String(fileName);
String p = GetZipPathWithoutFilename(sd);
IZipFolder* folder;
g_fs->GetFolder(p, (IFolder**)&folder);
IZipFile* file = new IZipFile();
file->FileName = GetFileName(sd, zipPathSplitter);
file->Provider = this;
file->Size = fileInfo.uncompressed_size;
file->EntryName = sd;
folder->AddFile(file);
}
unzCloseCurrentFile(g_sourceFile);
unzGoToNextFile(g_sourceFile);
}
//print folder tree
String tab(_text("\t"));
int cnt = g_fs->GetRootFolder()->folderCount;
for(int i = 0; i < cnt; i++)
{
IFolder* fld = g_fs->GetRootFolder()->GetFolder(i);
PRINT(_text("%s\n"), fld->Name.c_str());
PRINT(_text("Files count: %i\n"), fld->fileCount);
}
}
String IZipFileContentProvider::GetZipPathWithoutFilename(const String& path)
{
int offset = path.find_last_of(_text("/"));
if(offset>=0)
{
return path.substr(0, offset);
}
else
{
return _text("/");
}
}
}
+130
View File
@@ -0,0 +1,130 @@
#ifndef IZIPFILECONTENTPROVIDER_H__
#define IZIPFILECONTENTPROVIDER_H__
#include "../IContentProvider.h"
#include "../../LString.h"
#include "../IFileSystem.h"
//#include "test.h"
#include "../../minizip/unzip.h"
#include "IZipFolder.h"
namespace LifeCore
{
class IZipFileContentProvider : public IContentProvider
{
public:
~IZipFileContentProvider(){};
void Close(){}
IFileSystem* getFileSystem(){return g_fs;}
IZipFileContentProvider(const String& zipFile);
String g_rootFolder;
protected:
unz_global_info globalInfo;
IFileSystem* g_fs;
void BuildTree();
String GetZipPathWithoutFilename(const String& path);
public:
unzFile g_sourceFile;
/*private:
private string GetFilename(string path)
{
return System.IO.Path.GetFileName("C:\\" + path);
}
private IZipFolder BuildDirectory(string path)
{
string[] parts = path.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
IZipFolder folder = (IZipFolder)g_fs.GetRootFolder();
string npath = "";
for (int i = 0; i < parts.Length; i++)
{
npath+=parts[i] + "/";
IZipFolder f= (IZipFolder)folder.GetFolder(parts[i]);
if (f != null)
{
folder = f;
}
else
{
f = new IZipFolder();
f.EntryName = npath;
f.Name = parts[i];
f.Provider = this;
folder.AddFolder(f);
folder = f;
}
}
return folder;
}
private int ScanFolder(IFolder f)
{
string P = f.Path;
string[] dirs = System.IO.Directory.GetDirectories(g_rootFolder + P);
for (int i = 0; i < dirs.Length; i++)
{
}
string[] files = System.IO.Directory.GetFiles(g_rootFolder + P);
for (int i = 0; i < files.Length; i++)
{
IFSFile desc = new IFSFile();
desc.FileName = System.IO.Path.GetFileName(files[i]);
desc.Provider = this;
FileInfo info = new FileInfo(files[i]);
desc.Size = (int)info.Length;
desc.FSPath = files[i];
f.AddFile(desc);
}
return dirs.Length;
}
private string GetDirFromPath(string path)
{
string[] dirs = path.Split(System.IO.Path.DirectorySeparatorChar);
return dirs[dirs.Length - 1];
}
public:
ZipFile GetSource()
{
return g_sourceFile;
}
IFolder CreateFolder(string path)
{
return null;
}
IFileDescriptor CreateFile(string path)
{
System.IO.File.Create(path).Close();
IFSFile file = new IFSFile();
file.FileName = System.IO.Path.GetFileName(path);
file.FSPath = path;
file.Provider = this;
return file;
}
override void Close()
{
if (g_dataStream != null)
g_dataStream.Close();
}
override LifeEngine.FileSystem.IFileSystem FileSystem
{
get
{
return g_fs;
}
}*/
};
}
#endif
+31
View File
@@ -0,0 +1,31 @@
#include "IZipFolder.h"
namespace LifeCore
{
IZipFolder::~IZipFolder(void)
{
for( FolderMap::iterator it = g_folderCollection.begin(); it != g_folderCollection.end(); it++ )
delete it->second;
}
IFolder* IZipFolder::NewFolder(const String& name)
{
IZipFolder* newFolder = new IZipFolder();
newFolder->Name = name;
newFolder->setParent(this);
newFolder->EntryName = this->EntryName + name + _text("/");
newFolder->Provider = this->Provider;
this->AddFolder(newFolder);
return newFolder;
}
IFile* IZipFolder::NewFile(const String& name, int size)
{
UNREFERENCED_PARAMETER(size);
IFile* newFile = new IZipFile();
newFile->FileName = name;
newFile->Provider = this->Provider;
IZipFile* xf = (IZipFile*)newFile;
xf->EntryName = this->EntryName + name;
this->AddFile(newFile);
return newFile;
}
}
+25
View File
@@ -0,0 +1,25 @@
#ifndef IZIPFOLDER_H__
#define IZIPFOLDER_H__
#include "../../IDataStream.h"
#include "../../LString.h"
#include "../IFile.h"
#include "../IFolder.h"
#include "IZipFile.h"
#include "../IContentProvider.h"
#include <map>
namespace LifeCore
{
class IZipFolder : public IFolder
{
public:
~IZipFolder(void);
IContentProvider* Provider;
String EntryName;
IFolder* NewFolder(const String& name);
IFile* NewFile(const String& name, int size);
};
}
#endif
+108
View File
@@ -0,0 +1,108 @@
#ifndef IZIPSTREAM_H__
#define IZIPSTREAM_H__
#include "../../IDataStream.h"
#include "../../LString.h"
#include <assert.h>
#include "../../minizip/unzip.h"
#include "../../minizip/zip.h"
namespace LifeCore
{
class IZipStream : public IDataStream
{
private:
size_t stmLength;
unzFile source;
zipFile dest;
zip_fileinfo destInfo;
unz_file_info fileInfo;
int openMode;
public:
~IZipStream(){}
void Close()
{
if(openMode == FA_READ)
{
if(source)
{
unzCloseCurrentFile(source);
unzClose(source);
}
}
else //write
{
if(dest)
{
zipCloseFileInZip(dest);
zipClose(dest, "ONOTOLE NEGODUE");
}
}
openMode = 0;
}
IZipStream(unzFile& file, String& entry, int fileAccessMode, const String& archivePath)
{
source = file;
openMode = fileAccessMode;
if (fileAccessMode == FA_READ)
{
tchar fileName[256] = {0};
unzLocateFile(file, entry.c_str(), 1);
unzGetCurrentFileInfo(file,&fileInfo,fileName,256,NULL,0,NULL, 0);
stmLength = fileInfo.uncompressed_size;
unzOpenCurrentFile(file);
}
else
{
dest = zipOpen(archivePath.c_str(), 2);
destInfo.tmz_date.tm_sec = destInfo.tmz_date.tm_min = destInfo.tmz_date.tm_hour =
destInfo.tmz_date.tm_mday = destInfo.tmz_date.tm_mon = destInfo.tmz_date.tm_year = 0;
destInfo.dosDate = 0;
destInfo.internal_fa = 0;
destInfo.external_fa = 0;
#ifdef _UNICODE
char tmp[64] = {0};
wcstombs(tmp,entry.c_str(),entry.length());
zipOpenNewFileInZip(dest, tmp,&destInfo,NULL,0,NULL, 0, NULL, 0, 0);
#else
zipOpenNewFileInZip(dest, entry.c_str(),&destInfo,NULL,0,NULL, 0, NULL, 0, 0);
#endif
}
//g_openMode = mode;
}
size_t Read(void* pBuffer, size_t countBytes)
{
return unzReadCurrentFile(source, pBuffer, countBytes);
}
size_t Write(const void* pBuffer, size_t countBytes)
{
zipWriteInFileInZip(dest, pBuffer,countBytes);
return countBytes;
}
void Seek(size_t position)
{
printf("100500&%i\n", position);
printf("Unimplemented SEEK operation in IZipStream");
}
size_t Tell()
{
printf("Unimplemented TELL operation in IZipStream");
return 0;
}
size_t Length()
{
return stmLength;
}
bool CanRead()
{
return true;
}
bool CanWrite()
{
return true;
}
};
}
#endif