├── .gitignore ├── LICENSE.md ├── README.md ├── Source ├── ZipPlatformFile │ ├── Classes │ │ ├── ZipPlatformFileBlueprintFunctionLibrary.h │ │ └── ZipPlatformFileSettings.h │ ├── Private │ │ ├── ZipFileHandle.cpp │ │ ├── ZipFileHandle.h │ │ ├── ZipPlatformFile.cpp │ │ ├── ZipPlatformFile.h │ │ ├── ZipPlatformFileBlueprintFunctionLibrary.cpp │ │ ├── ZipPlatformFileGameInstanceSubsystem.cpp │ │ ├── ZipPlatformFileGameInstanceSubsystem.h │ │ ├── ZipPlatformFileModule.cpp │ │ ├── ZipPlatformFileModule.h │ │ ├── ZipPlatformFileSettings.cpp │ │ └── minizip │ │ │ ├── crypt.h │ │ │ ├── ioapi.c │ │ │ ├── ioapi.h │ │ │ ├── unzip.c │ │ │ ├── unzip.h │ │ │ ├── zip.c │ │ │ └── zip.h │ ├── Public │ │ └── IZipPlatformFile.h │ └── ZipPlatformFile.Build.cs └── ZipPlatformFileEd │ ├── Private │ ├── ZipPlatformFileEdModule.cpp │ └── ZipPlatformFileEdModule.h │ └── ZipPlatformFileEd.Build.cs └── ZipPlatformFile.uplugin /.gitignore: -------------------------------------------------------------------------------- 1 | .vs/ 2 | .vscode/ 3 | 4 | Binaries/ 5 | Intermediate/ 6 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright(c) 2021 Xing Ji (jixingcn@gmail.com). All Rights Reserved. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ZipPlatformFile 2 | 3 | ## About 4 | 5 | Allow to mount the zip file in memory. 6 | Reference [PakPlatformFile](https://docs.unrealengine.com/en-US/API/Runtime/PakFile/FPakPlatformFile/Mount/index.html). 7 | 8 | The plugin use [zlib](https://z-lib.org/) to mount the zip file. 9 | 10 | * You must use **UE4 API about file** when you operate the file. 11 | * The plugin **only unzip** the file in memory. 12 | * Support run in blueprint. 13 | 14 | ## Instruction 15 | 16 | * **New UE4 Plugin**: [English](https://jixing.me/en/posts/new-ue4-plugin-zipplatformfile/) [中文](https://jixing.me/posts/new-ue4-plugin-zipplatformfile/) 17 | * **New APIs**: [English](https://jixing.me/en/posts/new-apis-zipplatformfile/) [中文](https://jixing.me/posts/new-apis-zipplatformfile/) 18 | 19 | ```cpp 20 | UCLASS() 21 | class ZIPPLATFORMFILE_API UZipPlatformFileBlueprintFunctionLibrary : public UBlueprintFunctionLibrary 22 | { 23 | GENERATED_BODY() 24 | 25 | public: 26 | /// Check the mount status of a file 27 | UFUNCTION(BlueprintCallable, Category = "ZipPlatformFile", meta = (WorldContext = "WorldContextObject")) 28 | static bool IsMounted(UObject* WorldContextObject, const FString& Filename); 29 | 30 | /// Mount a file, you need pass the password if the file was encrypt 31 | UFUNCTION(BlueprintCallable, Category = "ZipPlatformFile", meta = (WorldContext = "WorldContextObject")) 32 | static bool Mount(UObject* WorldContextObject, const FString& MountPoint, const FString& Filename, const FString& Password); 33 | 34 | /// Unmount the file and free the memory 35 | UFUNCTION(BlueprintCallable, Category = "ZipPlatformFile", meta = (WorldContext = "WorldContextObject")) 36 | static bool Unmount(UObject* WorldContextObject, const FString& Filename); 37 | 38 | /// Check the status of a file in your zip files 39 | UFUNCTION(BlueprintCallable, Category = "ZipPlatformFile", meta = (WorldContext = "WorldContextObject")) 40 | static bool FileExists(UObject* WorldContextObject, const FString& Filename); 41 | 42 | /// Check the status of a directory in your zip files 43 | UFUNCTION(BlueprintCallable, Category = "ZipPlatformFile", meta = (WorldContext = "WorldContextObject")) 44 | static bool DirectoryExists(UObject* WorldContextObject, const FString& Directory); 45 | 46 | /// Find all files by `IZipPlatformFile` 47 | UFUNCTION(BlueprintCallable, Category = "ZipPlatformFile", meta = (WorldContext = "WorldContextObject")) 48 | static void FindFiles(UObject* WorldContextObject, TArray& FoundFiles, FString Directory, FString FileExtension, bool bRecursive = true); 49 | 50 | /// Find all directories by `IZipPlatformFile` 51 | UFUNCTION(BlueprintCallable, Category = "ZipPlatformFile", meta = (WorldContext = "WorldContextObject")) 52 | static void FindDirectories(UObject* WorldContextObject, TArray& FoundDirectories, FString Directory); 53 | 54 | /// Get the status of file or directory 55 | static bool GetFileStatData(UObject* WorldContextObject, const FString& FilenameOrDirectory, struct FFileStatData& OutFileStatData); 56 | 57 | /// Get the creation and access times of file or directory 58 | UFUNCTION(BlueprintCallable, Category = "ZipPlatformFile", meta = (WorldContext = "WorldContextObject")) 59 | static bool GetTimeStamp(UObject* WorldContextObject, const FString& FilenameOrDirectory, FDateTime& OutCreationTime, FDateTime& OutAccessTimeStamp); 60 | 61 | /// Load a file to array from your zip files 62 | static bool LoadFileToArray(UObject* WorldContextObject, const FString& Filename, TArray& Result); 63 | 64 | /// Load a file to string from your zip files 65 | UFUNCTION(BlueprintCallable, Category = "ZipPlatformFile", meta = (WorldContext = "WorldContextObject")) 66 | static bool LoadFileToString(UObject* WorldContextObject, const FString& Filename, FString& Result); 67 | }; 68 | ``` 69 | -------------------------------------------------------------------------------- /Source/ZipPlatformFile/Classes/ZipPlatformFileBlueprintFunctionLibrary.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include "ZipPlatformFileBlueprintFunctionLibrary.generated.h" 5 | 6 | UCLASS() 7 | class ZIPPLATFORMFILE_API UZipPlatformFileBlueprintFunctionLibrary : public UBlueprintFunctionLibrary 8 | { 9 | GENERATED_BODY() 10 | 11 | public: 12 | /// Check the mount status of a file 13 | UFUNCTION(BlueprintCallable, Category = "ZipPlatformFile", meta = (WorldContext = "WorldContextObject")) 14 | static bool IsMounted(UObject* WorldContextObject, const FString& Filename); 15 | 16 | /// Mount a file, you need pass the password if the file was encrypt 17 | UFUNCTION(BlueprintCallable, Category = "ZipPlatformFile", meta = (WorldContext = "WorldContextObject")) 18 | static bool Mount(UObject* WorldContextObject, const FString& MountPoint, const FString& Filename, const FString& Password); 19 | 20 | /// Unmount the file and free the memory 21 | UFUNCTION(BlueprintCallable, Category = "ZipPlatformFile", meta = (WorldContext = "WorldContextObject")) 22 | static bool Unmount(UObject* WorldContextObject, const FString& Filename); 23 | 24 | /// Check the status of a file in your zip files 25 | UFUNCTION(BlueprintCallable, Category = "ZipPlatformFile", meta = (WorldContext = "WorldContextObject")) 26 | static bool FileExists(UObject* WorldContextObject, const FString& Filename); 27 | 28 | /// Check the status of a directory in your zip files 29 | UFUNCTION(BlueprintCallable, Category = "ZipPlatformFile", meta = (WorldContext = "WorldContextObject")) 30 | static bool DirectoryExists(UObject* WorldContextObject, const FString& Directory); 31 | 32 | /// Find all files by `IZipPlatformFile` 33 | UFUNCTION(BlueprintCallable, Category = "ZipPlatformFile", meta = (WorldContext = "WorldContextObject")) 34 | static void FindFiles(UObject* WorldContextObject, TArray& FoundFiles, FString Directory, FString FileExtension, bool bRecursive = true); 35 | 36 | /// Find all directories by `IZipPlatformFile` 37 | UFUNCTION(BlueprintCallable, Category = "ZipPlatformFile", meta = (WorldContext = "WorldContextObject")) 38 | static void FindDirectories(UObject* WorldContextObject, TArray& FoundDirectories, FString Directory); 39 | 40 | /// Get the status of file or directory 41 | static bool GetFileStatData(UObject* WorldContextObject, const FString& FilenameOrDirectory, struct FFileStatData& OutFileStatData); 42 | 43 | /// Get the creation and access times of file or directory 44 | UFUNCTION(BlueprintCallable, Category = "ZipPlatformFile", meta = (WorldContext = "WorldContextObject")) 45 | static bool GetTimeStamp(UObject* WorldContextObject, const FString& FilenameOrDirectory, FDateTime& OutCreationTime, FDateTime& OutAccessTimeStamp); 46 | 47 | /// Load a file to array from your zip files 48 | static bool LoadFileToArray(UObject* WorldContextObject, const FString& Filename, TArray& Result); 49 | 50 | /// Load a file to string from your zip files 51 | UFUNCTION(BlueprintCallable, Category = "ZipPlatformFile", meta = (WorldContext = "WorldContextObject")) 52 | static bool LoadFileToString(UObject* WorldContextObject, const FString& Filename, FString& Result); 53 | }; 54 | -------------------------------------------------------------------------------- /Source/ZipPlatformFile/Classes/ZipPlatformFileSettings.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include "ZipPlatformFileSettings.generated.h" 5 | 6 | UCLASS(config = ZipPlatformFileSettings, defaultconfig) 7 | class ZIPPLATFORMFILE_API UZipPlatformFileSettings : public UObject 8 | { 9 | GENERATED_BODY() 10 | 11 | public: 12 | UZipPlatformFileSettings(); 13 | 14 | public: 15 | UPROPERTY(EditAnywhere, Category = "ZipPlatformFile") 16 | bool bSetPlatformFile; 17 | }; 18 | -------------------------------------------------------------------------------- /Source/ZipPlatformFile/Private/ZipFileHandle.cpp: -------------------------------------------------------------------------------- 1 | #include "ZipFileHandle.h" 2 | 3 | #include 4 | 5 | namespace ZipPlatformFile 6 | { 7 | voidpf zlib_open_file(voidpf opaque, const char* filename, int mode) 8 | { 9 | if (!opaque) 10 | return opaque; 11 | 12 | IFileHandle* FileHandlePtr = (IFileHandle*)opaque; 13 | return FileHandlePtr; 14 | } 15 | 16 | uLong zlib_read_file(voidpf opaque, voidpf stream, void* buf, uLong size) 17 | { 18 | if (!stream) 19 | return 0; 20 | 21 | IFileHandle* FileHandlePtr = (IFileHandle*)stream; 22 | const int64 RemainSize = FileHandlePtr->Size() - FileHandlePtr->Tell(); 23 | if (RemainSize <= 0) 24 | return 0; 25 | 26 | if (size > (uLong)RemainSize) 27 | size = (uLong)RemainSize; 28 | 29 | if (!FileHandlePtr->Read((uint8*)buf, size)) 30 | return 0; 31 | 32 | return size; 33 | } 34 | 35 | uLong zlib_write_file(voidpf opaque, voidpf stream, const void* buf, uLong size) 36 | { 37 | if (!stream) 38 | return 0; 39 | 40 | IFileHandle* FileHandlePtr = (IFileHandle*)stream; 41 | 42 | return 0; 43 | } 44 | 45 | long zlib_tell_file(voidpf opaque, voidpf stream) 46 | { 47 | if (!stream) 48 | return 0; 49 | 50 | IFileHandle* FileHandlePtr = (IFileHandle*)stream; 51 | return FileHandlePtr->Tell(); 52 | } 53 | 54 | long zlib_seek_file(voidpf opaque, voidpf stream, uLong offset, int origin) 55 | { 56 | if (!stream) 57 | return -1; 58 | 59 | IFileHandle* FileHandlePtr = (IFileHandle*)stream; 60 | switch (origin) 61 | { 62 | case ZLIB_FILEFUNC_SEEK_CUR: 63 | FileHandlePtr->Seek(FileHandlePtr->Tell() + (int64)offset); 64 | break; 65 | 66 | case ZLIB_FILEFUNC_SEEK_END: 67 | FileHandlePtr->SeekFromEnd((int64)offset); 68 | break; 69 | 70 | case ZLIB_FILEFUNC_SEEK_SET: 71 | FileHandlePtr->Seek((int64)offset); 72 | break; 73 | } 74 | 75 | return 0; 76 | } 77 | 78 | int zlib_close_file(voidpf opaque, voidpf stream) 79 | { 80 | // 81 | return 0; 82 | } 83 | 84 | int zlib_testerror_file(voidpf opaque, voidpf stream) 85 | { 86 | return 0; 87 | } 88 | } 89 | 90 | FZipFileHandle::FFileHandle::FFileHandle() 91 | : UncompressedData() 92 | , CurrentPos(0) 93 | { 94 | // 95 | } 96 | 97 | FZipFileHandle::FFileHandle::~FFileHandle() 98 | { 99 | // 100 | } 101 | 102 | int64 FZipFileHandle::FFileHandle::Tell() 103 | { 104 | return CurrentPos; 105 | } 106 | 107 | bool FZipFileHandle::FFileHandle::Seek(int64 NewPosition) 108 | { 109 | if (NewPosition < 0 || NewPosition > UncompressedData.Num()) 110 | return false; 111 | 112 | CurrentPos = NewPosition; 113 | return true; 114 | } 115 | 116 | bool FZipFileHandle::FFileHandle::SeekFromEnd(int64 NewPositionRelativeToEnd /*= 0*/) 117 | { 118 | if (NewPositionRelativeToEnd < 0 || NewPositionRelativeToEnd > UncompressedData.Num()) 119 | return false; 120 | 121 | CurrentPos = UncompressedData.Num() - NewPositionRelativeToEnd; 122 | return true; 123 | } 124 | 125 | bool FZipFileHandle::FFileHandle::Read(uint8* Destination, int64 BytesToRead) 126 | { 127 | if (BytesToRead <= 0 || (CurrentPos + BytesToRead) > UncompressedData.Num()) 128 | return false; 129 | 130 | FMemory::Memcpy(Destination, &UncompressedData[CurrentPos], BytesToRead); 131 | CurrentPos += BytesToRead; 132 | return true; 133 | } 134 | 135 | bool FZipFileHandle::FFileHandle::Write(const uint8* Source, int64 BytesToWrite) 136 | { 137 | /// not support 138 | return false; 139 | } 140 | 141 | bool FZipFileHandle::FFileHandle::Flush(const bool bFullFlush /*= false*/) 142 | { 143 | /// not support 144 | return false; 145 | } 146 | 147 | bool FZipFileHandle::FFileHandle::Truncate(int64 NewSize) 148 | { 149 | /// not support 150 | return false; 151 | } 152 | 153 | FZipFileHandle::FZipFileHandle(IPlatformFile* PlatformFilePtr, const TCHAR* MountPoint, const TCHAR* Filename, const TCHAR* Password /*= nullptr*/) 154 | : TheMountPoint(MountPoint) 155 | , TheFilename(Filename) 156 | , ThePassword(Password ? Password : TEXT("")) 157 | , FileHandlePtr(nullptr) 158 | , ZLibHandle(0) 159 | , ZLibFileInfoMap() 160 | { 161 | if (!PlatformFilePtr) 162 | return; 163 | 164 | if (!PlatformFilePtr->FileExists(Filename)) 165 | return; 166 | 167 | FileHandlePtr = PlatformFilePtr->OpenRead(Filename); 168 | if (!FileHandlePtr) 169 | return; 170 | 171 | if (!TheMountPoint.EndsWith(TEXT("/"))) 172 | TheMountPoint.AppendChar('/'); 173 | 174 | zlib_filefunc_def ZLibFileFunc; 175 | ZLibFileFunc.zopen_file = &ZipPlatformFile::zlib_open_file; 176 | ZLibFileFunc.zread_file = &ZipPlatformFile::zlib_read_file; 177 | ZLibFileFunc.zwrite_file = &ZipPlatformFile::zlib_write_file; 178 | ZLibFileFunc.ztell_file = &ZipPlatformFile::zlib_tell_file; 179 | ZLibFileFunc.zseek_file = &ZipPlatformFile::zlib_seek_file; 180 | ZLibFileFunc.zclose_file = &ZipPlatformFile::zlib_close_file; 181 | ZLibFileFunc.zerror_file = &ZipPlatformFile::zlib_testerror_file; 182 | ZLibFileFunc.opaque = FileHandlePtr; 183 | ZLibHandle = unzOpen2(TCHAR_TO_UTF8(Filename), &ZLibFileFunc); 184 | if (!ZLibHandle) 185 | return; 186 | 187 | /// scan the zip file 188 | int32 ZLibRes = unzGoToFirstFile(ZLibHandle); 189 | while (ZLibRes == UNZ_OK) 190 | { 191 | FZLibFileInfo ZLibFileInfo; 192 | ZLibRes = unzGetCurrentFileInfo(ZLibHandle, &ZLibFileInfo.Info, nullptr, 0, nullptr, 0, nullptr, 0); 193 | if (ZLibRes != UNZ_OK) 194 | break; 195 | if (ZLibFileInfo.Info.size_filename <= 0) 196 | break; 197 | 198 | std::vector ZLibFilenameTemp; 199 | ZLibFilenameTemp.resize(ZLibFileInfo.Info.size_filename + 1); 200 | ZLibFilenameTemp[ZLibFileInfo.Info.size_filename] = '\0'; 201 | 202 | ZLibRes = unzGetCurrentFileInfo(ZLibHandle, &ZLibFileInfo.Info, &ZLibFilenameTemp[0], ZLibFileInfo.Info.size_filename, nullptr, 0, nullptr, 0); 203 | if (ZLibRes != UNZ_OK) 204 | break; 205 | 206 | ZLibRes = unzGetFilePos(ZLibHandle, &ZLibFileInfo.Pos); 207 | if (ZLibRes != UNZ_OK) 208 | break; 209 | 210 | const FString ZLibFilename = UTF8_TO_TCHAR(&ZLibFilenameTemp[0]); 211 | if (!FZipFileHandle::GetParentDir(ZLibFileInfo.ParentDir, ZLibFilename)) 212 | continue; 213 | 214 | ZLibFileInfo.IsDirectory = ZLibFilename.EndsWith(TEXT("/")); 215 | ZLibFileInfoMap.Add(ZLibFilename, ZLibFileInfo); 216 | 217 | ZLibRes = unzGoToNextFile(ZLibHandle); 218 | } 219 | } 220 | 221 | FZipFileHandle::~FZipFileHandle() 222 | { 223 | if (ZLibHandle) 224 | { 225 | unzClose(ZLibHandle); 226 | ZLibHandle = 0; 227 | } 228 | 229 | if (FileHandlePtr) 230 | { 231 | delete FileHandlePtr; 232 | FileHandlePtr = nullptr; 233 | } 234 | } 235 | 236 | bool FZipFileHandle::GetFilename(const TCHAR* Filename, FString& OutFilename) const 237 | { 238 | const FString InputFilename = Filename; 239 | if (!InputFilename.StartsWith(TheMountPoint, ESearchCase::CaseSensitive)) 240 | return false; 241 | 242 | OutFilename = InputFilename.Mid(TheMountPoint.Len()); 243 | return true; 244 | } 245 | 246 | bool FZipFileHandle::GetRelativePath(const TCHAR *Directory, FString &OutRelativePath) const 247 | { 248 | const FString InputDirectory = Directory; 249 | if (InputDirectory.IsEmpty() || !InputDirectory.StartsWith(TheMountPoint)) 250 | return false; 251 | 252 | OutRelativePath = InputDirectory.Mid(TheMountPoint.Len()); 253 | return true; 254 | } 255 | 256 | bool FZipFileHandle::FileExists(const TCHAR* Filename) const 257 | { 258 | return ZLibFileInfoMap.Contains(Filename); 259 | } 260 | 261 | bool FZipFileHandle::DirectoryExists(const TCHAR* Directory) const 262 | { 263 | const FString TheDirectory = FZipFileHandle::FormatAsDirectoryPath(Directory); 264 | if (TheDirectory.IsEmpty()) 265 | return true; 266 | return ZLibFileInfoMap.Contains(TheDirectory); 267 | } 268 | 269 | int64 FZipFileHandle::FileSize(const TCHAR* Filename) const 270 | { 271 | const FZLibFileInfo* ZLibFileInfoPtr = ZLibFileInfoMap.Find(Filename); 272 | return ZLibFileInfoPtr ? (int64)ZLibFileInfoPtr->Info.uncompressed_size : 0; 273 | } 274 | 275 | FDateTime FZipFileHandle::GetTimeStamp(const TCHAR* Filename) const 276 | { 277 | return GetStatData(Filename).CreationTime; 278 | } 279 | 280 | IFileHandle* FZipFileHandle::OpenRead(const TCHAR* Filename, bool bAllowWrite /*= false*/) 281 | { 282 | const FZLibFileInfo* ZLibFileInfoPtr = ZLibFileInfoMap.Find(Filename); 283 | if (!ZLibFileInfoPtr) 284 | return nullptr; 285 | 286 | unz_file_pos TargetPos = ZLibFileInfoPtr->Pos; 287 | if (unzGoToFilePos(ZLibHandle, &TargetPos) != UNZ_OK) 288 | return nullptr; 289 | 290 | if ((ThePassword.IsEmpty() && unzOpenCurrentFile(ZLibHandle) != UNZ_OK) || 291 | (!ThePassword.IsEmpty() && unzOpenCurrentFilePassword(ZLibHandle, TCHAR_TO_UTF8(*ThePassword)) != UNZ_OK)) 292 | return nullptr; 293 | 294 | FFileHandle* OpenFileHandlePtr = new FFileHandle(); 295 | OpenFileHandlePtr->UncompressedData.SetNumUninitialized((int64)ZLibFileInfoPtr->Info.uncompressed_size); 296 | if (OpenFileHandlePtr->UncompressedData.Num() > 0) 297 | { 298 | if (unzReadCurrentFile(ZLibHandle, &OpenFileHandlePtr->UncompressedData[0], ZLibFileInfoPtr->Info.uncompressed_size) != ZLibFileInfoPtr->Info.uncompressed_size) 299 | { 300 | delete OpenFileHandlePtr; 301 | OpenFileHandlePtr = nullptr; 302 | } 303 | } 304 | unzCloseCurrentFile(ZLibHandle); 305 | return OpenFileHandlePtr; 306 | } 307 | 308 | FFileStatData FZipFileHandle::GetStatData(const TCHAR* FilenameOrDirectory) const 309 | { 310 | const FZLibFileInfo* ZLibFileInfoPtr = ZLibFileInfoMap.Find(FilenameOrDirectory); 311 | if (!ZLibFileInfoPtr) 312 | { 313 | /// try find as a directory 314 | const FString TheDirectory = FZipFileHandle::FormatAsDirectoryPath(FilenameOrDirectory); 315 | ZLibFileInfoPtr = ZLibFileInfoMap.Find(TheDirectory); 316 | } 317 | if (!ZLibFileInfoPtr) 318 | return FFileStatData(); 319 | 320 | const FDateTime FileDateTime = ZLibFileInfoPtr->GetDateTime(); 321 | return FFileStatData(FileDateTime, FileDateTime, FileDateTime, (int64)ZLibFileInfoPtr->Info.uncompressed_size, ZLibFileInfoPtr->IsDirectory, true); 322 | } 323 | 324 | void FZipFileHandle::FindFiles(TArray& FoundFiles, const TCHAR* Directory, const TCHAR* FileExtension, bool bRecursive) const 325 | { 326 | const FString TargetDirectory = Directory; 327 | const FString TargetFileExtension = FileExtension; 328 | for (const TPair& ZLibFileInfo : ZLibFileInfoMap) 329 | { 330 | if (ZLibFileInfo.Value.IsDirectory) 331 | continue; 332 | 333 | /// check the directory 334 | if (!bRecursive) 335 | if (!ZLibFileInfo.Value.ParentDir.Equals(TargetDirectory)) 336 | continue; 337 | else if (!TargetDirectory.IsEmpty() && !ZLibFileInfo.Key.StartsWith(TargetDirectory)) 338 | continue; 339 | 340 | /// check the extension 341 | if (!TargetFileExtension.IsEmpty() && !ZLibFileInfo.Key.EndsWith(TargetFileExtension)) 342 | continue; 343 | 344 | FoundFiles.Add(TheMountPoint / ZLibFileInfo.Key); 345 | } 346 | } 347 | 348 | bool FZipFileHandle::IterateDirectory(const TCHAR* Directory, IPlatformFile::FDirectoryVisitor& Visitor) 349 | { 350 | const FString TargetDirectory = Directory; 351 | for (const TPair& ZLibFileInfo : ZLibFileInfoMap) 352 | { 353 | if (TargetDirectory.IsEmpty() || ZLibFileInfo.Key.StartsWith(TargetDirectory)) 354 | Visitor.Visit(*ZLibFileInfo.Key, ZLibFileInfo.Value.IsDirectory); 355 | } 356 | return true; 357 | } 358 | 359 | bool FZipFileHandle::IterateDirectoryStat(const TCHAR* Directory, IPlatformFile::FDirectoryStatVisitor& Visitor) 360 | { 361 | const FString TargetDirectory = Directory; 362 | FFileStatData FileStatData; 363 | for (const TPair& ZLibFileInfo : ZLibFileInfoMap) 364 | { 365 | if (TargetDirectory.IsEmpty() || ZLibFileInfo.Key.StartsWith(TargetDirectory)) 366 | { 367 | const FZLibFileInfo& ZLibFileInfoValue = ZLibFileInfo.Value; 368 | FileStatData.CreationTime = ZLibFileInfoValue.GetDateTime(); 369 | FileStatData.AccessTime = FileStatData.CreationTime; 370 | FileStatData.ModificationTime = FileStatData.CreationTime; 371 | FileStatData.FileSize = ZLibFileInfoValue.Info.uncompressed_size; 372 | FileStatData.bIsDirectory = ZLibFileInfoValue.IsDirectory; 373 | FileStatData.bIsReadOnly = true; 374 | FileStatData.bIsValid = true; 375 | Visitor.Visit(*ZLibFileInfo.Key, FileStatData); 376 | } 377 | } 378 | return true; 379 | } 380 | 381 | FDateTime FZipFileHandle::FZLibFileInfo::GetDateTime() const 382 | { 383 | return FDateTime( 384 | (int32)Info.tmu_date.tm_year, 385 | (int32)Info.tmu_date.tm_mon + 1, 386 | (int32)Info.tmu_date.tm_mday, 387 | (int32)Info.tmu_date.tm_hour, 388 | (int32)Info.tmu_date.tm_min, 389 | (int32)Info.tmu_date.tm_sec 390 | ); 391 | } 392 | 393 | bool FZipFileHandle::GetParentDir(FString& ParentDir, FString FilenameOrDirectory) 394 | { 395 | if (FilenameOrDirectory.IsEmpty()) 396 | return false; 397 | if (FilenameOrDirectory[FilenameOrDirectory.Len() - 1] == '/') 398 | FilenameOrDirectory.RemoveAt(FilenameOrDirectory.Len() - 1); 399 | if (FilenameOrDirectory.IsEmpty()) 400 | return false; 401 | 402 | int32 LastBackSlant = INDEX_NONE; 403 | if (!FilenameOrDirectory.FindLastChar('/', LastBackSlant)) 404 | ParentDir = TEXT(""); 405 | else 406 | ParentDir = FilenameOrDirectory.Mid(0, LastBackSlant + 1); 407 | return true; 408 | } 409 | 410 | FString FZipFileHandle::FormatAsDirectoryPath(const TCHAR* Filename) 411 | { 412 | FString TheDirectory = Filename; 413 | if (!TheDirectory.IsEmpty()) 414 | { 415 | TheDirectory.ReplaceCharInline('\\', '/', ESearchCase::CaseSensitive); 416 | if (!TheDirectory.EndsWith(TEXT("/"))) 417 | TheDirectory.Append(TEXT("/")); 418 | } 419 | return TheDirectory; 420 | } 421 | -------------------------------------------------------------------------------- /Source/ZipPlatformFile/Private/ZipFileHandle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "minizip/zip.h" 4 | #include "minizip/unzip.h" 5 | #include 6 | 7 | class FZipFileHandle 8 | { 9 | class FFileHandle : public IFileHandle 10 | { 11 | friend FZipFileHandle; 12 | 13 | public: 14 | explicit FFileHandle(); 15 | virtual ~FFileHandle(); 16 | 17 | public: 18 | virtual int64 Tell() override; 19 | virtual bool Seek(int64 NewPosition) override; 20 | virtual bool SeekFromEnd(int64 NewPositionRelativeToEnd = 0) override; 21 | virtual bool Read(uint8* Destination, int64 BytesToRead) override; 22 | virtual bool Write(const uint8* Source, int64 BytesToWrite) override; 23 | virtual bool Flush(const bool bFullFlush = false) override; 24 | virtual bool Truncate(int64 NewSize) override; 25 | 26 | private: 27 | TArray64 UncompressedData; 28 | int64 CurrentPos; 29 | }; 30 | 31 | public: 32 | explicit FZipFileHandle(IPlatformFile* PlatformFilePtr, const TCHAR* MountPoint, const TCHAR* Filename, const TCHAR* Password = nullptr); 33 | virtual ~FZipFileHandle(); 34 | 35 | public: 36 | FORCEINLINE const FString& GetMountPoint() { return TheMountPoint; } 37 | FORCEINLINE const FString& GetFilename() { return TheFilename; } 38 | FORCEINLINE bool IsValid() const { return (ZLibHandle != 0); } 39 | bool GetFilename(const TCHAR* Filename, FString& OutFilename) const; 40 | bool GetRelativePath(const TCHAR* Directory, FString& OutRelativePath) const; 41 | 42 | public: 43 | bool FileExists(const TCHAR* Filename) const; 44 | bool DirectoryExists(const TCHAR* Directory) const; 45 | int64 FileSize(const TCHAR* Filename) const; 46 | FDateTime GetTimeStamp(const TCHAR* Filename) const; 47 | IFileHandle* OpenRead(const TCHAR* Filename, bool bAllowWrite = false); 48 | FFileStatData GetStatData(const TCHAR* FilenameOrDirectory) const; 49 | void FindFiles(TArray& FoundFiles, const TCHAR* Directory, const TCHAR* FileExtension, bool bRecursive) const; 50 | bool IterateDirectory(const TCHAR* Directory, IPlatformFile::FDirectoryVisitor& Visitor); 51 | bool IterateDirectoryStat(const TCHAR* Directory, IPlatformFile::FDirectoryStatVisitor& Visitor); 52 | 53 | private: 54 | FString TheMountPoint; 55 | FString TheFilename; 56 | FString ThePassword; 57 | IFileHandle* FileHandlePtr; 58 | unzFile ZLibHandle; 59 | struct FZLibFileInfo 60 | { 61 | unz_file_info Info; 62 | unz_file_pos Pos; 63 | FString ParentDir; 64 | bool IsDirectory; 65 | FDateTime GetDateTime() const; 66 | }; 67 | TMap ZLibFileInfoMap; 68 | 69 | public: 70 | static bool GetParentDir(FString& ParentDir, FString FilenameOrDirectory); 71 | static FString FormatAsDirectoryPath(const TCHAR* Filename); 72 | }; 73 | -------------------------------------------------------------------------------- /Source/ZipPlatformFile/Private/ZipPlatformFile.cpp: -------------------------------------------------------------------------------- 1 | #include "ZipPlatformFile.h" 2 | 3 | #include "ZipPlatformFileModule.h" 4 | #include "ZipFileHandle.h" 5 | 6 | FZipPlatformFile::FZipPlatformFile() 7 | : LowerLevelPtr(nullptr) 8 | , ZipFileHandlePtrs() 9 | { 10 | // 11 | } 12 | 13 | FZipPlatformFile::~FZipPlatformFile() 14 | { 15 | LowerLevelPtr = nullptr; 16 | } 17 | 18 | bool FZipPlatformFile::Initialize(IPlatformFile* Inner, const TCHAR* CmdLine) 19 | { 20 | LowerLevelPtr = Inner; 21 | return true; 22 | } 23 | 24 | IPlatformFile* FZipPlatformFile::GetLowerLevel() 25 | { 26 | return LowerLevelPtr; 27 | } 28 | 29 | void FZipPlatformFile::SetLowerLevel(IPlatformFile* NewLowerLevel) 30 | { 31 | LowerLevelPtr = NewLowerLevel; 32 | } 33 | 34 | const TCHAR* FZipPlatformFile::GetName() const 35 | { 36 | return TEXT("ZipPlatformFile"); 37 | } 38 | 39 | bool FZipPlatformFile::FileExists(const TCHAR* Filename) 40 | { 41 | FString ZipFilename; 42 | if (TSharedPtr FoundZipFileHandlePtr = GetZipFileHandle(Filename, ZipFilename)) 43 | return FoundZipFileHandlePtr->FileExists(*ZipFilename); 44 | 45 | return (LowerLevelPtr ? LowerLevelPtr->FileExists(Filename) : false); 46 | } 47 | 48 | int64 FZipPlatformFile::FileSize(const TCHAR* Filename) 49 | { 50 | FString ZipFilename; 51 | if (TSharedPtr FoundZipFileHandlePtr = GetZipFileHandle(Filename, ZipFilename)) 52 | FoundZipFileHandlePtr->FileSize(*ZipFilename); 53 | 54 | return (LowerLevelPtr ? LowerLevelPtr->FileSize(Filename) : 0); 55 | } 56 | 57 | bool FZipPlatformFile::DeleteFile(const TCHAR* Filename) 58 | { 59 | FString ZipFilename; 60 | if (TSharedPtr FoundZipFileHandlePtr = GetZipFileHandle(Filename, ZipFilename)) 61 | return false; 62 | 63 | return (LowerLevelPtr ? LowerLevelPtr->DeleteFile(Filename) : false); 64 | } 65 | 66 | bool FZipPlatformFile::IsReadOnly(const TCHAR* Filename) 67 | { 68 | FString ZipFilename; 69 | if (TSharedPtr FoundZipFileHandlePtr = GetZipFileHandle(Filename, ZipFilename)) 70 | return true; 71 | 72 | return (LowerLevelPtr ? LowerLevelPtr->IsReadOnly(Filename) : false); 73 | } 74 | 75 | bool FZipPlatformFile::MoveFile(const TCHAR* To, const TCHAR* From) 76 | { 77 | FString ZipFilename; 78 | if (TSharedPtr FoundZipFileHandlePtr = GetZipFileHandle(To, ZipFilename)) 79 | return false; 80 | if (TSharedPtr FoundZipFileHandlePtr = GetZipFileHandle(From, ZipFilename)) 81 | return false; 82 | 83 | return (LowerLevelPtr ? LowerLevelPtr->MoveFile(To, From) : false); 84 | } 85 | 86 | bool FZipPlatformFile::SetReadOnly(const TCHAR* Filename, bool bNewReadOnlyValue) 87 | { 88 | FString ZipFilename; 89 | if (TSharedPtr FoundZipFileHandlePtr = GetZipFileHandle(Filename, ZipFilename)) 90 | return false; 91 | 92 | return (LowerLevelPtr ? LowerLevelPtr->SetReadOnly(Filename, bNewReadOnlyValue) : false); 93 | } 94 | 95 | FDateTime FZipPlatformFile::GetTimeStamp(const TCHAR* Filename) 96 | { 97 | FString ZipFilename; 98 | if (TSharedPtr FoundZipFileHandlePtr = GetZipFileHandle(Filename, ZipFilename)) 99 | return FoundZipFileHandlePtr->GetTimeStamp(*ZipFilename); 100 | 101 | return (LowerLevelPtr ? LowerLevelPtr->GetTimeStamp(Filename) : FDateTime::MinValue()); 102 | } 103 | 104 | void FZipPlatformFile::SetTimeStamp(const TCHAR* Filename, FDateTime DateTime) 105 | { 106 | FString ZipFilename; 107 | if (TSharedPtr FoundZipFileHandlePtr = GetZipFileHandle(Filename, ZipFilename)) 108 | return; 109 | 110 | if (LowerLevelPtr) 111 | LowerLevelPtr->SetTimeStamp(Filename, DateTime); 112 | } 113 | 114 | FDateTime FZipPlatformFile::GetAccessTimeStamp(const TCHAR* Filename) 115 | { 116 | FString ZipFilename; 117 | if (TSharedPtr FoundZipFileHandlePtr = GetZipFileHandle(Filename, ZipFilename)) 118 | return FoundZipFileHandlePtr->GetTimeStamp(*ZipFilename); 119 | 120 | return (LowerLevelPtr ? LowerLevelPtr->GetAccessTimeStamp(Filename) : FDateTime::MinValue()); 121 | } 122 | 123 | FString FZipPlatformFile::GetFilenameOnDisk(const TCHAR* Filename) 124 | { 125 | FString ZipFilename; 126 | if (TSharedPtr FoundZipFileHandlePtr = GetZipFileHandle(Filename, ZipFilename)) 127 | return FString(Filename); 128 | 129 | return (LowerLevelPtr ? LowerLevelPtr->GetFilenameOnDisk(Filename) : TEXT("")); 130 | } 131 | 132 | IFileHandle* FZipPlatformFile::OpenRead(const TCHAR* Filename, bool bAllowWrite /*= false*/) 133 | { 134 | FString ZipFilename; 135 | if (TSharedPtr FoundZipFileHandlePtr = GetZipFileHandle(Filename, ZipFilename)) 136 | return FoundZipFileHandlePtr->OpenRead(*ZipFilename, bAllowWrite); 137 | 138 | return (LowerLevelPtr ? LowerLevelPtr->OpenRead(Filename, bAllowWrite) : nullptr); 139 | } 140 | 141 | IFileHandle* FZipPlatformFile::OpenWrite(const TCHAR* Filename, bool bAppend /*= false*/, bool bAllowRead /*= false*/) 142 | { 143 | FString ZipFilename; 144 | if (TSharedPtr FoundZipFileHandlePtr = GetZipFileHandle(Filename, ZipFilename)) 145 | return false; 146 | 147 | return (LowerLevelPtr ? LowerLevelPtr->OpenWrite(Filename, bAppend, bAllowRead) : nullptr); 148 | } 149 | 150 | bool FZipPlatformFile::DirectoryExists(const TCHAR* Directory) 151 | { 152 | FString ZipFilename; 153 | if (TSharedPtr FoundZipFileHandlePtr = GetZipFileHandle(Directory, ZipFilename)) 154 | return FoundZipFileHandlePtr->DirectoryExists(*ZipFilename); 155 | 156 | if (!LowerLevelPtr) 157 | return false; 158 | 159 | return (LowerLevelPtr ? LowerLevelPtr->DirectoryExists(Directory) : false); 160 | } 161 | 162 | bool FZipPlatformFile::CreateDirectory(const TCHAR* Directory) 163 | { 164 | FString ZipFilename; 165 | if (TSharedPtr FoundZipFileHandlePtr = GetZipFileHandle(Directory, ZipFilename)) 166 | return false; 167 | 168 | return (LowerLevelPtr ? LowerLevelPtr->CreateDirectory(Directory) : false); 169 | } 170 | 171 | bool FZipPlatformFile::DeleteDirectory(const TCHAR* Directory) 172 | { 173 | FString ZipFilename; 174 | if (TSharedPtr FoundZipFileHandlePtr = GetZipFileHandle(Directory, ZipFilename)) 175 | return false; 176 | 177 | return (LowerLevelPtr ? LowerLevelPtr->DeleteDirectory(Directory) : false); 178 | } 179 | 180 | FFileStatData FZipPlatformFile::GetStatData(const TCHAR* FilenameOrDirectory) 181 | { 182 | FString ZipFilename; 183 | if (TSharedPtr FoundZipFileHandlePtr = GetZipFileHandle(FilenameOrDirectory, ZipFilename)) 184 | return FoundZipFileHandlePtr->GetStatData(*ZipFilename); 185 | 186 | return (LowerLevelPtr ? LowerLevelPtr->GetStatData(FilenameOrDirectory) : FFileStatData()); 187 | } 188 | 189 | void FZipPlatformFile::FindFiles(TArray& FoundFiles, const TCHAR* Directory, const TCHAR* FileExtension) 190 | { 191 | if (LowerLevelPtr->DirectoryExists(Directory)) 192 | LowerLevelPtr->FindFiles(FoundFiles, Directory, FileExtension); 193 | FindFilesInternal(FoundFiles, Directory, FileExtension, false); 194 | } 195 | 196 | void FZipPlatformFile::FindFilesRecursively(TArray& FoundFiles, const TCHAR* Directory, const TCHAR* FileExtension) 197 | { 198 | if (LowerLevelPtr->DirectoryExists(Directory)) 199 | LowerLevelPtr->FindFilesRecursively(FoundFiles, Directory, FileExtension); 200 | FindFilesInternal(FoundFiles, Directory, FileExtension, true); 201 | } 202 | 203 | bool FZipPlatformFile::IterateDirectory(const TCHAR* Directory, IPlatformFile::FDirectoryVisitor& Visitor) 204 | { 205 | FString ZipFilename; 206 | if (TSharedPtr FoundZipFileHandlePtr = GetZipFileHandle(Directory, ZipFilename)) 207 | return FoundZipFileHandlePtr->IterateDirectory(*ZipFilename, Visitor); 208 | 209 | return (LowerLevelPtr ? LowerLevelPtr->IterateDirectory(Directory, Visitor) : false); 210 | } 211 | 212 | bool FZipPlatformFile::IterateDirectoryStat(const TCHAR* Directory, IPlatformFile::FDirectoryStatVisitor& Visitor) 213 | { 214 | FString ZipFilename; 215 | if (TSharedPtr FoundZipFileHandlePtr = GetZipFileHandle(Directory, ZipFilename)) 216 | return FoundZipFileHandlePtr->IterateDirectoryStat(*ZipFilename, Visitor); 217 | 218 | return (LowerLevelPtr ? LowerLevelPtr->IterateDirectoryStat(Directory, Visitor) : false); 219 | } 220 | 221 | bool FZipPlatformFile::IsMounted(const TCHAR* Filename) const 222 | { 223 | FString TheFilename = Filename; 224 | FPaths::MakeStandardFilename(TheFilename); 225 | 226 | return ZipFileHandlePtrs.Contains(TheFilename); 227 | } 228 | 229 | bool FZipPlatformFile::Mount(const TCHAR* MountPoint, const TCHAR* Filename, const TCHAR* Password /*= nullptr*/) 230 | { 231 | FString TheFilename = Filename; 232 | FPaths::MakeStandardFilename(TheFilename); 233 | 234 | if (IsMounted(*TheFilename)) 235 | return false; 236 | 237 | TSharedPtr ZipFileHandlePtr = MakeShared(LowerLevelPtr, MountPoint, *TheFilename, Password); 238 | if (!ZipFileHandlePtr->IsValid()) 239 | return false; 240 | 241 | ZipFileHandlePtrs.Add(TheFilename, ZipFileHandlePtr); 242 | return true; 243 | } 244 | 245 | bool FZipPlatformFile::Unmount(const TCHAR* Filename) 246 | { 247 | FString TheFilename = Filename; 248 | FPaths::MakeStandardFilename(TheFilename); 249 | 250 | if (!IsMounted(*TheFilename)) 251 | return false; 252 | 253 | ZipFileHandlePtrs.Remove(TheFilename); 254 | return true; 255 | } 256 | 257 | TSharedPtr FZipPlatformFile::GetZipFileHandle(const TCHAR* Filename, FString& OutZipFilename) const 258 | { 259 | for (const TPair>& ZipFileHandlePtr : ZipFileHandlePtrs) 260 | { 261 | if (!ZipFileHandlePtr.Value->GetFilename(Filename, OutZipFilename)) 262 | continue; 263 | return ZipFileHandlePtr.Value; 264 | } 265 | return nullptr; 266 | } 267 | 268 | void FZipPlatformFile::FindFilesInternal(TArray& FoundFiles, const TCHAR* Directory, const TCHAR* FileExtension, bool bRecursive) const 269 | { 270 | FString ZipFilename; 271 | if (TSharedPtr FoundZipFileHandlePtr = GetZipFileHandle(Directory, ZipFilename)) 272 | { 273 | FString RelativePath; 274 | if (FoundZipFileHandlePtr->GetRelativePath(Directory, RelativePath)) 275 | FoundZipFileHandlePtr->FindFiles(FoundFiles, *RelativePath, FileExtension, bRecursive); 276 | } 277 | } 278 | -------------------------------------------------------------------------------- /Source/ZipPlatformFile/Private/ZipPlatformFile.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "IZipPlatformFile.h" 4 | 5 | class FZipPlatformFile : public IZipPlatformFile 6 | { 7 | public: 8 | FZipPlatformFile(); 9 | virtual ~FZipPlatformFile(); 10 | 11 | public: 12 | virtual bool Initialize(IPlatformFile* Inner, const TCHAR* CmdLine) override; 13 | virtual IPlatformFile* GetLowerLevel() override; 14 | virtual void SetLowerLevel(IPlatformFile* NewLowerLevel) override; 15 | virtual const TCHAR* GetName() const override; 16 | 17 | public: 18 | using IPlatformFile::IterateDirectory; 19 | using IPlatformFile::IterateDirectoryRecursively; 20 | 21 | public: 22 | virtual bool FileExists(const TCHAR* Filename) override; 23 | virtual int64 FileSize(const TCHAR* Filename) override; 24 | virtual bool DeleteFile(const TCHAR* Filename) override; 25 | virtual bool IsReadOnly(const TCHAR* Filename) override; 26 | virtual bool MoveFile(const TCHAR* To, const TCHAR* From) override; 27 | virtual bool SetReadOnly(const TCHAR* Filename, bool bNewReadOnlyValue) override; 28 | virtual FDateTime GetTimeStamp(const TCHAR* Filename) override; 29 | virtual void SetTimeStamp(const TCHAR* Filename, FDateTime DateTime) override; 30 | virtual FDateTime GetAccessTimeStamp(const TCHAR* Filename) override; 31 | virtual FString GetFilenameOnDisk(const TCHAR* Filename) override; 32 | virtual IFileHandle* OpenRead(const TCHAR* Filename, bool bAllowWrite = false) override; 33 | virtual IFileHandle* OpenWrite(const TCHAR* Filename, bool bAppend = false, bool bAllowRead = false) override; 34 | virtual bool DirectoryExists(const TCHAR* Directory) override; 35 | virtual bool CreateDirectory(const TCHAR* Directory) override; 36 | virtual bool DeleteDirectory(const TCHAR* Directory) override; 37 | virtual FFileStatData GetStatData(const TCHAR* FilenameOrDirectory) override; 38 | virtual void FindFiles(TArray& FoundFiles, const TCHAR* Directory, const TCHAR* FileExtension) override; 39 | virtual void FindFilesRecursively(TArray& FoundFiles, const TCHAR* Directory, const TCHAR* FileExtension) override; 40 | virtual bool IterateDirectory(const TCHAR* Directory, IPlatformFile::FDirectoryVisitor& Visitor) override; 41 | virtual bool IterateDirectoryStat(const TCHAR* Directory, IPlatformFile::FDirectoryStatVisitor& Visitor) override; 42 | 43 | public: 44 | virtual bool IsMounted(const TCHAR* Filename) const override; 45 | virtual bool Mount(const TCHAR* MountPoint, const TCHAR* Filename, const TCHAR* Password = nullptr) override; 46 | virtual bool Unmount(const TCHAR* Filename) override; 47 | 48 | protected: 49 | TSharedPtr GetZipFileHandle(const TCHAR* Filename, FString& OutZipFilename) const; 50 | void FindFilesInternal(TArray& FoundFiles, const TCHAR* Directory, const TCHAR* FileExtension, bool bRecursive) const; 51 | 52 | private: 53 | IPlatformFile* LowerLevelPtr; 54 | TMap> ZipFileHandlePtrs; 55 | }; 56 | -------------------------------------------------------------------------------- /Source/ZipPlatformFile/Private/ZipPlatformFileBlueprintFunctionLibrary.cpp: -------------------------------------------------------------------------------- 1 | #include "ZipPlatformFileBlueprintFunctionLibrary.h" 2 | 3 | #include "ZipPlatformFileGameInstanceSubsystem.h" 4 | 5 | namespace ZipPlatformFile 6 | { 7 | IZipPlatformFile* GetZipPlatformFile(UObject* WorldContextObject) 8 | { 9 | checkSlow(WorldContextObject); 10 | if (!WorldContextObject) 11 | return nullptr; 12 | 13 | UWorld* WorldPtr = WorldContextObject->GetWorld(); 14 | checkSlow(WorldPtr); 15 | if (!WorldPtr) 16 | return nullptr; 17 | 18 | UGameInstance* GameInstancePtr = WorldPtr->GetGameInstance(); 19 | checkSlow(GameInstancePtr); 20 | if (!GameInstancePtr) 21 | return nullptr; 22 | 23 | UZipPlatformFileGameInstanceSubsystem* ZipPlatformFileGameInstanceSubsystemPtr = GameInstancePtr->GetSubsystem(); 24 | checkSlow(ZipPlatformFileGameInstanceSubsystemPtr); 25 | if (!ZipPlatformFileGameInstanceSubsystemPtr) 26 | return nullptr; 27 | 28 | return ZipPlatformFileGameInstanceSubsystemPtr->GetZipPlatformFile(); 29 | } 30 | } 31 | 32 | bool UZipPlatformFileBlueprintFunctionLibrary::IsMounted(UObject* WorldContextObject, const FString& Filename) 33 | { 34 | IZipPlatformFile* ZipPlatformFilePtr = ZipPlatformFile::GetZipPlatformFile(WorldContextObject); 35 | checkSlow(ZipPlatformFilePtr); 36 | if (!ZipPlatformFilePtr) 37 | return false; 38 | 39 | return ZipPlatformFilePtr->IsMounted(*Filename); 40 | } 41 | 42 | bool UZipPlatformFileBlueprintFunctionLibrary::Mount(UObject* WorldContextObject, const FString& MountPoint, const FString& Filename, const FString& Password) 43 | { 44 | IZipPlatformFile* ZipPlatformFilePtr = ZipPlatformFile::GetZipPlatformFile(WorldContextObject); 45 | checkSlow(ZipPlatformFilePtr); 46 | if (!ZipPlatformFilePtr) 47 | return false; 48 | 49 | return ZipPlatformFilePtr->Mount(*MountPoint, *Filename, Password.IsEmpty() ? nullptr : *Password); 50 | } 51 | 52 | bool UZipPlatformFileBlueprintFunctionLibrary::Unmount(UObject* WorldContextObject, const FString& Filename) 53 | { 54 | IZipPlatformFile* ZipPlatformFilePtr = ZipPlatformFile::GetZipPlatformFile(WorldContextObject); 55 | checkSlow(ZipPlatformFilePtr); 56 | if (!ZipPlatformFilePtr) 57 | return false; 58 | 59 | return ZipPlatformFilePtr->Unmount(*Filename); 60 | } 61 | 62 | bool UZipPlatformFileBlueprintFunctionLibrary::FileExists(UObject* WorldContextObject, const FString& Filename) 63 | { 64 | return FPaths::FileExists(Filename); 65 | } 66 | 67 | bool UZipPlatformFileBlueprintFunctionLibrary::DirectoryExists(UObject* WorldContextObject, const FString& Directory) 68 | { 69 | return FPaths::DirectoryExists(Directory); 70 | } 71 | 72 | void UZipPlatformFileBlueprintFunctionLibrary::FindFiles(UObject* WorldContextObject, TArray& FoundFiles, FString Directory, FString FileExtension, bool bRecursive /*= true*/) 73 | { 74 | IZipPlatformFile* ZipPlatformFilePtr = ZipPlatformFile::GetZipPlatformFile(WorldContextObject); 75 | checkSlow(ZipPlatformFilePtr); 76 | if (!ZipPlatformFilePtr) 77 | return; 78 | 79 | /// format the file extension 80 | if (!FileExtension.IsEmpty() && FileExtension[0] != '.') 81 | FileExtension.InsertAt(0, '.'); 82 | 83 | /// format the directory 84 | if (Directory.IsEmpty() || Directory[Directory.Len() - 1] != '/') 85 | Directory.AppendChar('/'); 86 | 87 | if (!bRecursive) 88 | ZipPlatformFilePtr->FindFiles(FoundFiles, *Directory, *FileExtension); 89 | else 90 | ZipPlatformFilePtr->FindFilesRecursively(FoundFiles, *Directory, *FileExtension); 91 | } 92 | 93 | void UZipPlatformFileBlueprintFunctionLibrary::FindDirectories(UObject* WorldContextObject, TArray& FoundDirectories, FString Directory) 94 | { 95 | IZipPlatformFile* ZipPlatformFilePtr = ZipPlatformFile::GetZipPlatformFile(WorldContextObject); 96 | checkSlow(ZipPlatformFilePtr); 97 | if (!ZipPlatformFilePtr) 98 | return; 99 | 100 | class FZipDirectoryVisitor : public IPlatformFile::FDirectoryVisitor 101 | { 102 | public: 103 | FZipDirectoryVisitor(TArray& FoundDirectories) 104 | : IPlatformFile::FDirectoryVisitor() 105 | , FoundDirectories(FoundDirectories) 106 | { 107 | // 108 | } 109 | 110 | public: 111 | virtual bool Visit(const TCHAR* FilenameOrDirectory, bool bIsDirectory) 112 | { 113 | if (bIsDirectory) 114 | FoundDirectories.Add(FString(FilenameOrDirectory)); 115 | return true; 116 | } 117 | 118 | private: 119 | TArray& FoundDirectories; 120 | }; 121 | 122 | /// format the directory 123 | if (Directory.IsEmpty() || Directory[Directory.Len() - 1] != '/') 124 | Directory.AppendChar('/'); 125 | 126 | FZipDirectoryVisitor ZipDirectoryVisitor(FoundDirectories); 127 | ZipPlatformFilePtr->IterateDirectory(*Directory, ZipDirectoryVisitor); 128 | } 129 | 130 | bool UZipPlatformFileBlueprintFunctionLibrary::GetFileStatData(UObject* WorldContextObject, const FString& FilenameOrDirectory, FFileStatData& OutFileStatData) 131 | { 132 | IZipPlatformFile* ZipPlatformFilePtr = ZipPlatformFile::GetZipPlatformFile(WorldContextObject); 133 | checkSlow(ZipPlatformFilePtr); 134 | if (!ZipPlatformFilePtr) 135 | return false; 136 | if (!ZipPlatformFilePtr->FileExists(*FilenameOrDirectory) && !ZipPlatformFilePtr->DirectoryExists(*FilenameOrDirectory)) 137 | return false; 138 | OutFileStatData = ZipPlatformFilePtr->GetStatData(*FilenameOrDirectory); 139 | return true; 140 | } 141 | 142 | bool UZipPlatformFileBlueprintFunctionLibrary::GetTimeStamp(UObject* WorldContextObject, const FString& FilenameOrDirectory, FDateTime& OutCreationTime, FDateTime& OutAccessTimeStamp) 143 | { 144 | FFileStatData FileStatData; 145 | if (!UZipPlatformFileBlueprintFunctionLibrary::GetFileStatData(WorldContextObject, FilenameOrDirectory, FileStatData)) 146 | return false; 147 | OutCreationTime = FileStatData.CreationTime; 148 | OutAccessTimeStamp = FileStatData.AccessTime; 149 | return true; 150 | } 151 | 152 | bool UZipPlatformFileBlueprintFunctionLibrary::LoadFileToArray(UObject* WorldContextObject, const FString& Filename, TArray& Result) 153 | { 154 | return FFileHelper::LoadFileToArray(Result, *Filename); 155 | } 156 | 157 | bool UZipPlatformFileBlueprintFunctionLibrary::LoadFileToString(UObject* WorldContextObject, const FString& Filename, FString& Result) 158 | { 159 | return FFileHelper::LoadFileToString(Result, *Filename); 160 | } 161 | 162 | namespace ZipPlatformFile 163 | { 164 | static FAutoConsoleCommandWithWorldAndArgs CZipPlatformFileIsMounted( 165 | TEXT("ZipPlatformFile.IsMounted"), 166 | TEXT(""), 167 | FConsoleCommandWithWorldAndArgsDelegate::CreateLambda([](const TArray& InArgs, UWorld* InWorld) { 168 | if (InArgs.Num() != 1) 169 | return; 170 | UZipPlatformFileBlueprintFunctionLibrary::IsMounted(InWorld, InArgs[0]); 171 | }), 172 | ECVF_Cheat); 173 | 174 | static FAutoConsoleCommandWithWorldAndArgs CZipPlatformFileMount( 175 | TEXT("ZipPlatformFile.Mount"), 176 | TEXT(""), 177 | FConsoleCommandWithWorldAndArgsDelegate::CreateLambda([](const TArray& InArgs, UWorld* InWorld) { 178 | if (InArgs.Num() < 2) 179 | return; 180 | const FString Password = InArgs.Num() > 2 ? InArgs[2] : TEXT(""); 181 | UZipPlatformFileBlueprintFunctionLibrary::Mount(InWorld, InArgs[0], InArgs[1], Password); 182 | }), 183 | ECVF_Cheat); 184 | 185 | static FAutoConsoleCommandWithWorldAndArgs CZipPlatformFileUnmount( 186 | TEXT("ZipPlatformFile.Unmount"), 187 | TEXT(""), 188 | FConsoleCommandWithWorldAndArgsDelegate::CreateLambda([](const TArray& InArgs, UWorld* InWorld) { 189 | if (InArgs.Num() != 1) 190 | return; 191 | UZipPlatformFileBlueprintFunctionLibrary::Unmount(InWorld, InArgs[0]); 192 | }), 193 | ECVF_Cheat); 194 | 195 | static FAutoConsoleCommandWithWorldAndArgs CZipPlatformFileFileExists( 196 | TEXT("ZipPlatformFile.FileExists"), 197 | TEXT(""), 198 | FConsoleCommandWithWorldAndArgsDelegate::CreateLambda([](const TArray& InArgs, UWorld* InWorld) { 199 | if (InArgs.Num() < 1) 200 | return; 201 | UZipPlatformFileBlueprintFunctionLibrary::FileExists(InWorld, InArgs[0]); 202 | }), 203 | ECVF_Cheat); 204 | 205 | static FAutoConsoleCommandWithWorldAndArgs CZipPlatformFileDirectoryExists( 206 | TEXT("ZipPlatformFile.DirectoryExists"), 207 | TEXT(""), 208 | FConsoleCommandWithWorldAndArgsDelegate::CreateLambda([](const TArray& InArgs, UWorld* InWorld) { 209 | if (InArgs.Num() < 1) 210 | return; 211 | UZipPlatformFileBlueprintFunctionLibrary::DirectoryExists(InWorld, InArgs[0]); 212 | }), 213 | ECVF_Cheat); 214 | 215 | static FAutoConsoleCommandWithWorldAndArgs CZipPlatformFileFindFiles( 216 | TEXT("ZipPlatformFile.FindFiles"), 217 | TEXT(""), 218 | FConsoleCommandWithWorldAndArgsDelegate::CreateLambda([](const TArray& InArgs, UWorld* InWorld) { 219 | if (InArgs.Num() < 1) 220 | return; 221 | 222 | TArray FoundDirectories; 223 | const FString FileExtension = InArgs.Num() > 1 ? InArgs[1] : TEXT(""); 224 | const bool bRecursive = InArgs.Num() > 2 ? InArgs[2].Equals(TEXT("true"), ESearchCase::IgnoreCase) : false; 225 | UZipPlatformFileBlueprintFunctionLibrary::FindFiles(InWorld, FoundDirectories, *InArgs[0], FileExtension, bRecursive); 226 | }), 227 | ECVF_Cheat); 228 | 229 | static FAutoConsoleCommandWithWorldAndArgs CZipPlatformFileFindDirectories( 230 | TEXT("ZipPlatformFile.FindDirectories"), 231 | TEXT(""), 232 | FConsoleCommandWithWorldAndArgsDelegate::CreateLambda([](const TArray& InArgs, UWorld* InWorld) { 233 | if (InArgs.Num() < 1) 234 | return; 235 | 236 | TArray FoundDirectories; 237 | UZipPlatformFileBlueprintFunctionLibrary::FindDirectories(InWorld, FoundDirectories, *InArgs[0]); 238 | }), 239 | ECVF_Cheat); 240 | 241 | static FAutoConsoleCommandWithWorldAndArgs CZipPlatformFileLoadFileToArray( 242 | TEXT("ZipPlatformFile.LoadFileToArray"), 243 | TEXT(""), 244 | FConsoleCommandWithWorldAndArgsDelegate::CreateLambda([](const TArray& InArgs, UWorld* InWorld) { 245 | if (InArgs.Num() < 1) 246 | return; 247 | TArray Result; 248 | UZipPlatformFileBlueprintFunctionLibrary::LoadFileToArray(InWorld, InArgs[0], Result); 249 | }), 250 | ECVF_Cheat); 251 | 252 | static FAutoConsoleCommandWithWorldAndArgs CZipPlatformFileLoadFileToString( 253 | TEXT("ZipPlatformFile.LoadFileToString"), 254 | TEXT(""), 255 | FConsoleCommandWithWorldAndArgsDelegate::CreateLambda([](const TArray& InArgs, UWorld* InWorld) { 256 | if (InArgs.Num() < 1) 257 | return; 258 | FString Result; 259 | UZipPlatformFileBlueprintFunctionLibrary::LoadFileToString(InWorld, InArgs[0], Result); 260 | }), 261 | ECVF_Cheat); 262 | 263 | static FAutoConsoleCommandWithWorldAndArgs CZipPlatformFileGetTimeStampAndStatData( 264 | TEXT("ZipPlatformFile.GetFileStatData"), 265 | TEXT(""), 266 | FConsoleCommandWithWorldAndArgsDelegate::CreateLambda([](const TArray& InArgs, UWorld* InWorld) { 267 | if (InArgs.Num() < 1) 268 | return; 269 | 270 | FFileStatData FileStatData; 271 | UZipPlatformFileBlueprintFunctionLibrary::GetFileStatData(InWorld, *InArgs[0], FileStatData); 272 | }), 273 | ECVF_Cheat); 274 | } 275 | -------------------------------------------------------------------------------- /Source/ZipPlatformFile/Private/ZipPlatformFileGameInstanceSubsystem.cpp: -------------------------------------------------------------------------------- 1 | #include "ZipPlatformFileGameInstanceSubsystem.h" 2 | 3 | #include "ZipPlatformFile.h" 4 | #include "ZipPlatformFileSettings.h" 5 | 6 | #include 7 | 8 | UZipPlatformFileGameInstanceSubsystem::UZipPlatformFileGameInstanceSubsystem() 9 | : Super() 10 | , ZipPlatformFilePtr(nullptr) 11 | { 12 | // 13 | } 14 | 15 | void UZipPlatformFileGameInstanceSubsystem::Initialize(FSubsystemCollectionBase& Collection) 16 | { 17 | Super::Initialize(Collection); 18 | 19 | FPlatformFileManager& PlatformFileManagerRef = FPlatformFileManager::Get(); 20 | 21 | ZipPlatformFilePtr = MakeUnique(); 22 | ZipPlatformFilePtr->Initialize(&PlatformFileManagerRef.GetPlatformFile(), nullptr); 23 | 24 | const UZipPlatformFileSettings* ZipPlatformFileSettingsPtr = GetDefault(); 25 | check(ZipPlatformFileSettingsPtr); 26 | if (ZipPlatformFileSettingsPtr->bSetPlatformFile) 27 | { 28 | PlatformFileManagerRef.SetPlatformFile(*ZipPlatformFilePtr.Get()); 29 | } 30 | } 31 | 32 | void UZipPlatformFileGameInstanceSubsystem::Deinitialize() 33 | { 34 | FPlatformFileManager& PlatformFileManagerRef = FPlatformFileManager::Get(); 35 | 36 | const UZipPlatformFileSettings* ZipPlatformFileSettingsPtr = GetDefault(); 37 | check(ZipPlatformFileSettingsPtr); 38 | if (ZipPlatformFileSettingsPtr->bSetPlatformFile) 39 | { 40 | PlatformFileManagerRef.SetPlatformFile(*ZipPlatformFilePtr->GetLowerLevel()); 41 | } 42 | ZipPlatformFilePtr = nullptr; 43 | 44 | Super::Deinitialize(); 45 | } 46 | -------------------------------------------------------------------------------- /Source/ZipPlatformFile/Private/ZipPlatformFileGameInstanceSubsystem.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "IZipPlatformFile.h" 4 | 5 | #include 6 | #include "ZipPlatformFileGameInstanceSubsystem.generated.h" 7 | 8 | UCLASS() 9 | class UZipPlatformFileGameInstanceSubsystem : public UGameInstanceSubsystem 10 | { 11 | GENERATED_BODY() 12 | 13 | public: 14 | UZipPlatformFileGameInstanceSubsystem(); 15 | 16 | public: 17 | virtual void Initialize(FSubsystemCollectionBase& Collection) override; 18 | virtual void Deinitialize() override; 19 | 20 | public: 21 | FORCEINLINE IZipPlatformFile* GetZipPlatformFile() const 22 | { 23 | return ZipPlatformFilePtr.Get(); 24 | } 25 | 26 | private: 27 | TUniquePtr ZipPlatformFilePtr; 28 | }; 29 | -------------------------------------------------------------------------------- /Source/ZipPlatformFile/Private/ZipPlatformFileModule.cpp: -------------------------------------------------------------------------------- 1 | #include "ZipPlatformFileModule.h" 2 | 3 | #include "ZipPlatformFile.h" 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | DEFINE_LOG_CATEGORY(LogZipPlatformFile); 10 | 11 | class FZipPlatformFileModule : public IModuleInterface 12 | { 13 | public: 14 | FZipPlatformFileModule() 15 | { 16 | // 17 | } 18 | 19 | virtual ~FZipPlatformFileModule() 20 | { 21 | // 22 | } 23 | 24 | public: 25 | virtual void StartupModule() 26 | { 27 | // 28 | } 29 | 30 | virtual void ShutdownModule() 31 | { 32 | // 33 | } 34 | }; 35 | 36 | IMPLEMENT_MODULE(FZipPlatformFileModule, ZipPlatformFile); 37 | -------------------------------------------------------------------------------- /Source/ZipPlatformFile/Private/ZipPlatformFileModule.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | DECLARE_LOG_CATEGORY_EXTERN(LogZipPlatformFile, Log, All); 6 | -------------------------------------------------------------------------------- /Source/ZipPlatformFile/Private/ZipPlatformFileSettings.cpp: -------------------------------------------------------------------------------- 1 | #include "ZipPlatformFileSettings.h" 2 | 3 | UZipPlatformFileSettings::UZipPlatformFileSettings() 4 | : Super() 5 | , bSetPlatformFile(true) 6 | { 7 | // 8 | } 9 | -------------------------------------------------------------------------------- /Source/ZipPlatformFile/Private/minizip/crypt.h: -------------------------------------------------------------------------------- 1 | /* crypt.h -- base code for crypt/uncrypt ZIPfile 2 | 3 | 4 | Version 1.01e, February 12th, 2005 5 | 6 | Copyright (C) 1998-2005 Gilles Vollant 7 | 8 | This code is a modified version of crypting code in Infozip distribution 9 | 10 | The encryption/decryption parts of this source code (as opposed to the 11 | non-echoing password parts) were originally written in Europe. The 12 | whole source package can be freely distributed, including from the USA. 13 | (Prior to January 2000, re-export from the US was a violation of US law.) 14 | 15 | This encryption code is a direct transcription of the algorithm from 16 | Roger Schlafly, described by Phil Katz in the file appnote.txt. This 17 | file (appnote.txt) is distributed with the PKZIP program (even in the 18 | version without encryption capabilities). 19 | 20 | If you don't need crypting in your application, just define symbols 21 | NOCRYPT and NOUNCRYPT. 22 | 23 | This code support the "Traditional PKWARE Encryption". 24 | 25 | The new AES encryption added on Zip format by Winzip (see the page 26 | http://www.winzip.com/aes_info.htm ) and PKWare PKZip 5.x Strong 27 | Encryption is not supported. 28 | */ 29 | 30 | #define CRC32(c, b) ((*(pcrc_32_tab+(((int)(c) ^ (b)) & 0xff))) ^ ((c) >> 8)) 31 | 32 | /*********************************************************************** 33 | * Return the next byte in the pseudo-random sequence 34 | */ 35 | static int decrypt_byte(unsigned long* pkeys, const z_crc_t* pcrc_32_tab) 36 | { 37 | unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an 38 | * unpredictable manner on 16-bit systems; not a problem 39 | * with any known compiler so far, though */ 40 | 41 | temp = ((unsigned)(*(pkeys+2)) & 0xffff) | 2; 42 | return (int)(((temp * (temp ^ 1)) >> 8) & 0xff); 43 | } 44 | 45 | /*********************************************************************** 46 | * Update the encryption keys with the next byte of plain text 47 | */ 48 | static int update_keys(unsigned long* pkeys,const z_crc_t* pcrc_32_tab,int c) 49 | { 50 | (*(pkeys+0)) = CRC32((*(pkeys+0)), c); 51 | (*(pkeys+1)) += (*(pkeys+0)) & 0xff; 52 | (*(pkeys+1)) = (*(pkeys+1)) * 134775813L + 1; 53 | { 54 | register int keyshift = (int)((*(pkeys+1)) >> 24); 55 | (*(pkeys+2)) = CRC32((*(pkeys+2)), keyshift); 56 | } 57 | return c; 58 | } 59 | 60 | 61 | /*********************************************************************** 62 | * Initialize the encryption keys and the random header according to 63 | * the given password. 64 | */ 65 | static void init_keys(const char* passwd,unsigned long* pkeys,const z_crc_t* pcrc_32_tab) 66 | { 67 | *(pkeys+0) = 305419896L; 68 | *(pkeys+1) = 591751049L; 69 | *(pkeys+2) = 878082192L; 70 | while (*passwd != '\0') { 71 | update_keys(pkeys,pcrc_32_tab,(int)*passwd); 72 | passwd++; 73 | } 74 | } 75 | 76 | #define zdecode(pkeys,pcrc_32_tab,c) \ 77 | (update_keys(pkeys,pcrc_32_tab,c ^= decrypt_byte(pkeys,pcrc_32_tab))) 78 | 79 | #define zencode(pkeys,pcrc_32_tab,c,t) \ 80 | (t=decrypt_byte(pkeys,pcrc_32_tab), update_keys(pkeys,pcrc_32_tab,c), t^(c)) 81 | 82 | #ifdef INCLUDECRYPTINGCODE_IFCRYPTALLOWED 83 | 84 | #define RAND_HEAD_LEN 12 85 | /* "last resort" source for second part of crypt seed pattern */ 86 | # ifndef ZCR_SEED2 87 | # define ZCR_SEED2 3141592654UL /* use PI as default pattern */ 88 | # endif 89 | 90 | static int crypthead(const char* passwd, /* password string */ 91 | unsigned char* buf, /* where to write header */ 92 | int bufSize, 93 | unsigned long* pkeys, 94 | const z_crc_t* pcrc_32_tab, 95 | unsigned long crcForCrypting) 96 | { 97 | int n; /* index in random header */ 98 | int t; /* temporary */ 99 | int c; /* random byte */ 100 | unsigned char header[RAND_HEAD_LEN-2]; /* random header */ 101 | static unsigned calls = 0; /* ensure different random header each time */ 102 | 103 | if (bufSize> 7) & 0xff; 118 | header[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, c, t); 119 | } 120 | /* Encrypt random header (last two bytes is high word of crc) */ 121 | init_keys(passwd, pkeys, pcrc_32_tab); 122 | for (n = 0; n < RAND_HEAD_LEN-2; n++) 123 | { 124 | buf[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, header[n], t); 125 | } 126 | buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 16) & 0xff, t); 127 | buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 24) & 0xff, t); 128 | return n; 129 | } 130 | 131 | #endif 132 | -------------------------------------------------------------------------------- /Source/ZipPlatformFile/Private/minizip/ioapi.c: -------------------------------------------------------------------------------- 1 | /* ioapi.h -- IO base function header for compress/uncompress .zip 2 | part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html ) 3 | 4 | Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html ) 5 | 6 | Modifications for Zip64 support 7 | Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com ) 8 | 9 | For more info read MiniZip_info.txt 10 | 11 | */ 12 | 13 | #if (defined(_WIN32)) 14 | #define _CRT_SECURE_NO_WARNINGS 15 | #endif 16 | 17 | #include "ioapi.h" 18 | 19 | voidpf call_zopen64 (const zlib_filefunc64_32_def* pfilefunc,const void*filename,int mode) 20 | { 21 | if (pfilefunc->zfile_func64.zopen64_file != NULL) 22 | return (*(pfilefunc->zfile_func64.zopen64_file)) (pfilefunc->zfile_func64.opaque,filename,mode); 23 | else 24 | { 25 | return (*(pfilefunc->zopen32_file))(pfilefunc->zfile_func64.opaque,(const char*)filename,mode); 26 | } 27 | } 28 | 29 | long call_zseek64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream, ZPOS64_T offset, int origin) 30 | { 31 | if (pfilefunc->zfile_func64.zseek64_file != NULL) 32 | return (*(pfilefunc->zfile_func64.zseek64_file)) (pfilefunc->zfile_func64.opaque,filestream,offset,origin); 33 | else 34 | { 35 | uLong offsetTruncated = (uLong)offset; 36 | if (offsetTruncated != offset) 37 | return -1; 38 | else 39 | return (*(pfilefunc->zseek32_file))(pfilefunc->zfile_func64.opaque,filestream,offsetTruncated,origin); 40 | } 41 | } 42 | 43 | ZPOS64_T call_ztell64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream) 44 | { 45 | if (pfilefunc->zfile_func64.zseek64_file != NULL) 46 | return (*(pfilefunc->zfile_func64.ztell64_file)) (pfilefunc->zfile_func64.opaque,filestream); 47 | else 48 | { 49 | uLong tell_uLong = (*(pfilefunc->ztell32_file))(pfilefunc->zfile_func64.opaque,filestream); 50 | if ((tell_uLong) == ((uLong)-1)) 51 | return (ZPOS64_T)-1; 52 | else 53 | return tell_uLong; 54 | } 55 | } 56 | 57 | void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def* p_filefunc64_32,const zlib_filefunc_def* p_filefunc32) 58 | { 59 | p_filefunc64_32->zfile_func64.zopen64_file = NULL; 60 | p_filefunc64_32->zopen32_file = p_filefunc32->zopen_file; 61 | p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file; 62 | p_filefunc64_32->zfile_func64.zread_file = p_filefunc32->zread_file; 63 | p_filefunc64_32->zfile_func64.zwrite_file = p_filefunc32->zwrite_file; 64 | p_filefunc64_32->zfile_func64.ztell64_file = NULL; 65 | p_filefunc64_32->zfile_func64.zseek64_file = NULL; 66 | p_filefunc64_32->zfile_func64.zclose_file = p_filefunc32->zclose_file; 67 | p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file; 68 | p_filefunc64_32->zfile_func64.opaque = p_filefunc32->opaque; 69 | p_filefunc64_32->zseek32_file = p_filefunc32->zseek_file; 70 | p_filefunc64_32->ztell32_file = p_filefunc32->ztell_file; 71 | } 72 | 73 | 74 | 75 | static voidpf ZCALLBACK fopen_file_func OF((voidpf opaque, const char* filename, int mode)); 76 | static uLong ZCALLBACK fread_file_func OF((voidpf opaque, voidpf stream, void* buf, uLong size)); 77 | static uLong ZCALLBACK fwrite_file_func OF((voidpf opaque, voidpf stream, const void* buf,uLong size)); 78 | static ZPOS64_T ZCALLBACK ftell64_file_func OF((voidpf opaque, voidpf stream)); 79 | static long ZCALLBACK fseek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)); 80 | static int ZCALLBACK fclose_file_func OF((voidpf opaque, voidpf stream)); 81 | static int ZCALLBACK ferror_file_func OF((voidpf opaque, voidpf stream)); 82 | 83 | static voidpf ZCALLBACK fopen_file_func (voidpf opaque, const char* filename, int mode) 84 | { 85 | FILE* file = NULL; 86 | const char* mode_fopen = NULL; 87 | if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ) 88 | mode_fopen = "rb"; 89 | else 90 | if (mode & ZLIB_FILEFUNC_MODE_EXISTING) 91 | mode_fopen = "r+b"; 92 | else 93 | if (mode & ZLIB_FILEFUNC_MODE_CREATE) 94 | mode_fopen = "wb"; 95 | 96 | if ((filename!=NULL) && (mode_fopen != NULL)) 97 | file = fopen(filename, mode_fopen); 98 | return file; 99 | } 100 | 101 | static voidpf ZCALLBACK fopen64_file_func (voidpf opaque, const void* filename, int mode) 102 | { 103 | FILE* file = NULL; 104 | const char* mode_fopen = NULL; 105 | if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ) 106 | mode_fopen = "rb"; 107 | else 108 | if (mode & ZLIB_FILEFUNC_MODE_EXISTING) 109 | mode_fopen = "r+b"; 110 | else 111 | if (mode & ZLIB_FILEFUNC_MODE_CREATE) 112 | mode_fopen = "wb"; 113 | 114 | if ((filename!=NULL) && (mode_fopen != NULL)) 115 | file = fopen64((const char*)filename, mode_fopen); 116 | return file; 117 | } 118 | 119 | 120 | static uLong ZCALLBACK fread_file_func (voidpf opaque, voidpf stream, void* buf, uLong size) 121 | { 122 | uLong ret; 123 | ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream); 124 | return ret; 125 | } 126 | 127 | static uLong ZCALLBACK fwrite_file_func (voidpf opaque, voidpf stream, const void* buf, uLong size) 128 | { 129 | uLong ret; 130 | ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream); 131 | return ret; 132 | } 133 | 134 | static long ZCALLBACK ftell_file_func (voidpf opaque, voidpf stream) 135 | { 136 | long ret; 137 | ret = ftell((FILE *)stream); 138 | return ret; 139 | } 140 | 141 | 142 | static ZPOS64_T ZCALLBACK ftell64_file_func (voidpf opaque, voidpf stream) 143 | { 144 | ZPOS64_T ret; 145 | ret = ftello64((FILE *)stream); 146 | return ret; 147 | } 148 | 149 | static long ZCALLBACK fseek_file_func (voidpf opaque, voidpf stream, uLong offset, int origin) 150 | { 151 | int fseek_origin=0; 152 | long ret; 153 | switch (origin) 154 | { 155 | case ZLIB_FILEFUNC_SEEK_CUR : 156 | fseek_origin = SEEK_CUR; 157 | break; 158 | case ZLIB_FILEFUNC_SEEK_END : 159 | fseek_origin = SEEK_END; 160 | break; 161 | case ZLIB_FILEFUNC_SEEK_SET : 162 | fseek_origin = SEEK_SET; 163 | break; 164 | default: return -1; 165 | } 166 | ret = 0; 167 | if (fseek((FILE *)stream, offset, fseek_origin) != 0) 168 | ret = -1; 169 | return ret; 170 | } 171 | 172 | static long ZCALLBACK fseek64_file_func (voidpf opaque, voidpf stream, ZPOS64_T offset, int origin) 173 | { 174 | int fseek_origin=0; 175 | long ret; 176 | switch (origin) 177 | { 178 | case ZLIB_FILEFUNC_SEEK_CUR : 179 | fseek_origin = SEEK_CUR; 180 | break; 181 | case ZLIB_FILEFUNC_SEEK_END : 182 | fseek_origin = SEEK_END; 183 | break; 184 | case ZLIB_FILEFUNC_SEEK_SET : 185 | fseek_origin = SEEK_SET; 186 | break; 187 | default: return -1; 188 | } 189 | ret = 0; 190 | 191 | if(fseeko64((FILE *)stream, offset, fseek_origin) != 0) 192 | ret = -1; 193 | 194 | return ret; 195 | } 196 | 197 | 198 | static int ZCALLBACK fclose_file_func (voidpf opaque, voidpf stream) 199 | { 200 | int ret; 201 | ret = fclose((FILE *)stream); 202 | return ret; 203 | } 204 | 205 | static int ZCALLBACK ferror_file_func (voidpf opaque, voidpf stream) 206 | { 207 | int ret; 208 | ret = ferror((FILE *)stream); 209 | return ret; 210 | } 211 | 212 | void fill_fopen_filefunc (pzlib_filefunc_def) 213 | zlib_filefunc_def* pzlib_filefunc_def; 214 | { 215 | pzlib_filefunc_def->zopen_file = fopen_file_func; 216 | pzlib_filefunc_def->zread_file = fread_file_func; 217 | pzlib_filefunc_def->zwrite_file = fwrite_file_func; 218 | pzlib_filefunc_def->ztell_file = ftell_file_func; 219 | pzlib_filefunc_def->zseek_file = fseek_file_func; 220 | pzlib_filefunc_def->zclose_file = fclose_file_func; 221 | pzlib_filefunc_def->zerror_file = ferror_file_func; 222 | pzlib_filefunc_def->opaque = NULL; 223 | } 224 | 225 | void fill_fopen64_filefunc (zlib_filefunc64_def* pzlib_filefunc_def) 226 | { 227 | pzlib_filefunc_def->zopen64_file = fopen64_file_func; 228 | pzlib_filefunc_def->zread_file = fread_file_func; 229 | pzlib_filefunc_def->zwrite_file = fwrite_file_func; 230 | pzlib_filefunc_def->ztell64_file = ftell64_file_func; 231 | pzlib_filefunc_def->zseek64_file = fseek64_file_func; 232 | pzlib_filefunc_def->zclose_file = fclose_file_func; 233 | pzlib_filefunc_def->zerror_file = ferror_file_func; 234 | pzlib_filefunc_def->opaque = NULL; 235 | } 236 | -------------------------------------------------------------------------------- /Source/ZipPlatformFile/Private/minizip/ioapi.h: -------------------------------------------------------------------------------- 1 | /* ioapi.h -- IO base function header for compress/uncompress .zip 2 | part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html ) 3 | 4 | Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html ) 5 | 6 | Modifications for Zip64 support 7 | Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com ) 8 | 9 | For more info read MiniZip_info.txt 10 | 11 | Changes 12 | 13 | Oct-2009 - Defined ZPOS64_T to fpos_t on windows and u_int64_t on linux. (might need to find a better why for this) 14 | Oct-2009 - Change to fseeko64, ftello64 and fopen64 so large files would work on linux. 15 | More if/def section may be needed to support other platforms 16 | Oct-2009 - Defined fxxxx64 calls to normal fopen/ftell/fseek so they would compile on windows. 17 | (but you should use iowin32.c for windows instead) 18 | 19 | */ 20 | 21 | #ifndef _ZLIBIOAPI64_H 22 | #define _ZLIBIOAPI64_H 23 | 24 | #if (!defined(_WIN32)) && (!defined(WIN32)) 25 | 26 | // Linux needs this to support file operation on files larger then 4+GB 27 | // But might need better if/def to select just the platforms that needs them. 28 | 29 | #ifndef __USE_FILE_OFFSET64 30 | #define __USE_FILE_OFFSET64 31 | #endif 32 | #ifndef __USE_LARGEFILE64 33 | #define __USE_LARGEFILE64 34 | #endif 35 | #ifndef _LARGEFILE64_SOURCE 36 | #define _LARGEFILE64_SOURCE 37 | #endif 38 | #ifndef _FILE_OFFSET_BIT 39 | #define _FILE_OFFSET_BIT 64 40 | #endif 41 | #endif 42 | 43 | #include 44 | #include 45 | #include "zlib.h" 46 | 47 | #if defined(USE_FILE32API) 48 | #define fopen64 fopen 49 | #define ftello64 ftell 50 | #define fseeko64 fseek 51 | #else 52 | #ifdef _MSC_VER 53 | #define fopen64 fopen 54 | #if (_MSC_VER >= 1400) && (!(defined(NO_MSCVER_FILE64_FUNC))) 55 | #define ftello64 _ftelli64 56 | #define fseeko64 _fseeki64 57 | #else // old MSC 58 | #define ftello64 ftell 59 | #define fseeko64 fseek 60 | #endif 61 | #endif 62 | #endif 63 | 64 | /* 65 | #ifndef ZPOS64_T 66 | #ifdef _WIN32 67 | #define ZPOS64_T fpos_t 68 | #else 69 | #include 70 | #define ZPOS64_T uint64_t 71 | #endif 72 | #endif 73 | */ 74 | 75 | #ifdef HAVE_MINIZIP64_CONF_H 76 | #include "mz64conf.h" 77 | #endif 78 | 79 | /* a type choosen by DEFINE */ 80 | #ifdef HAVE_64BIT_INT_CUSTOM 81 | typedef 64BIT_INT_CUSTOM_TYPE ZPOS64_T; 82 | #else 83 | #ifdef HAS_STDINT_H 84 | #include "stdint.h" 85 | typedef uint64_t ZPOS64_T; 86 | #else 87 | 88 | 89 | #if defined(_MSC_VER) || defined(__BORLANDC__) 90 | typedef unsigned __int64 ZPOS64_T; 91 | #else 92 | typedef unsigned long long int ZPOS64_T; 93 | #endif 94 | #endif 95 | #endif 96 | 97 | 98 | 99 | #ifdef __cplusplus 100 | extern "C" { 101 | #endif 102 | 103 | 104 | #define ZLIB_FILEFUNC_SEEK_CUR (1) 105 | #define ZLIB_FILEFUNC_SEEK_END (2) 106 | #define ZLIB_FILEFUNC_SEEK_SET (0) 107 | 108 | #define ZLIB_FILEFUNC_MODE_READ (1) 109 | #define ZLIB_FILEFUNC_MODE_WRITE (2) 110 | #define ZLIB_FILEFUNC_MODE_READWRITEFILTER (3) 111 | 112 | #define ZLIB_FILEFUNC_MODE_EXISTING (4) 113 | #define ZLIB_FILEFUNC_MODE_CREATE (8) 114 | 115 | 116 | #ifndef ZCALLBACK 117 | #if (defined(WIN32) || defined(_WIN32) || defined (WINDOWS) || defined (_WINDOWS)) && defined(CALLBACK) && defined (USEWINDOWS_CALLBACK) 118 | #define ZCALLBACK CALLBACK 119 | #else 120 | #define ZCALLBACK 121 | #endif 122 | #endif 123 | 124 | 125 | 126 | 127 | typedef voidpf (ZCALLBACK *open_file_func) OF((voidpf opaque, const char* filename, int mode)); 128 | typedef uLong (ZCALLBACK *read_file_func) OF((voidpf opaque, voidpf stream, void* buf, uLong size)); 129 | typedef uLong (ZCALLBACK *write_file_func) OF((voidpf opaque, voidpf stream, const void* buf, uLong size)); 130 | typedef int (ZCALLBACK *close_file_func) OF((voidpf opaque, voidpf stream)); 131 | typedef int (ZCALLBACK *testerror_file_func) OF((voidpf opaque, voidpf stream)); 132 | 133 | typedef long (ZCALLBACK *tell_file_func) OF((voidpf opaque, voidpf stream)); 134 | typedef long (ZCALLBACK *seek_file_func) OF((voidpf opaque, voidpf stream, uLong offset, int origin)); 135 | 136 | 137 | /* here is the "old" 32 bits structure structure */ 138 | typedef struct zlib_filefunc_def_s 139 | { 140 | open_file_func zopen_file; 141 | read_file_func zread_file; 142 | write_file_func zwrite_file; 143 | tell_file_func ztell_file; 144 | seek_file_func zseek_file; 145 | close_file_func zclose_file; 146 | testerror_file_func zerror_file; 147 | voidpf opaque; 148 | } zlib_filefunc_def; 149 | 150 | typedef ZPOS64_T (ZCALLBACK *tell64_file_func) OF((voidpf opaque, voidpf stream)); 151 | typedef long (ZCALLBACK *seek64_file_func) OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)); 152 | typedef voidpf (ZCALLBACK *open64_file_func) OF((voidpf opaque, const void* filename, int mode)); 153 | 154 | typedef struct zlib_filefunc64_def_s 155 | { 156 | open64_file_func zopen64_file; 157 | read_file_func zread_file; 158 | write_file_func zwrite_file; 159 | tell64_file_func ztell64_file; 160 | seek64_file_func zseek64_file; 161 | close_file_func zclose_file; 162 | testerror_file_func zerror_file; 163 | voidpf opaque; 164 | } zlib_filefunc64_def; 165 | 166 | void fill_fopen64_filefunc OF((zlib_filefunc64_def* pzlib_filefunc_def)); 167 | void fill_fopen_filefunc OF((zlib_filefunc_def* pzlib_filefunc_def)); 168 | 169 | /* now internal definition, only for zip.c and unzip.h */ 170 | typedef struct zlib_filefunc64_32_def_s 171 | { 172 | zlib_filefunc64_def zfile_func64; 173 | open_file_func zopen32_file; 174 | tell_file_func ztell32_file; 175 | seek_file_func zseek32_file; 176 | } zlib_filefunc64_32_def; 177 | 178 | 179 | #define ZREAD64(filefunc,filestream,buf,size) ((*((filefunc).zfile_func64.zread_file)) ((filefunc).zfile_func64.opaque,filestream,buf,size)) 180 | #define ZWRITE64(filefunc,filestream,buf,size) ((*((filefunc).zfile_func64.zwrite_file)) ((filefunc).zfile_func64.opaque,filestream,buf,size)) 181 | //#define ZTELL64(filefunc,filestream) ((*((filefunc).ztell64_file)) ((filefunc).opaque,filestream)) 182 | //#define ZSEEK64(filefunc,filestream,pos,mode) ((*((filefunc).zseek64_file)) ((filefunc).opaque,filestream,pos,mode)) 183 | #define ZCLOSE64(filefunc,filestream) ((*((filefunc).zfile_func64.zclose_file)) ((filefunc).zfile_func64.opaque,filestream)) 184 | #define ZERROR64(filefunc,filestream) ((*((filefunc).zfile_func64.zerror_file)) ((filefunc).zfile_func64.opaque,filestream)) 185 | 186 | voidpf call_zopen64 OF((const zlib_filefunc64_32_def* pfilefunc,const void*filename,int mode)); 187 | long call_zseek64 OF((const zlib_filefunc64_32_def* pfilefunc,voidpf filestream, ZPOS64_T offset, int origin)); 188 | ZPOS64_T call_ztell64 OF((const zlib_filefunc64_32_def* pfilefunc,voidpf filestream)); 189 | 190 | void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def* p_filefunc64_32,const zlib_filefunc_def* p_filefunc32); 191 | 192 | #define ZOPEN64(filefunc,filename,mode) (call_zopen64((&(filefunc)),(filename),(mode))) 193 | #define ZTELL64(filefunc,filestream) (call_ztell64((&(filefunc)),(filestream))) 194 | #define ZSEEK64(filefunc,filestream,pos,mode) (call_zseek64((&(filefunc)),(filestream),(pos),(mode))) 195 | 196 | #ifdef __cplusplus 197 | } 198 | #endif 199 | 200 | #endif 201 | -------------------------------------------------------------------------------- /Source/ZipPlatformFile/Private/minizip/unzip.h: -------------------------------------------------------------------------------- 1 | /* unzip.h -- IO for uncompress .zip files using zlib 2 | Version 1.1, February 14h, 2010 3 | part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html ) 4 | 5 | Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html ) 6 | 7 | Modifications of Unzip for Zip64 8 | Copyright (C) 2007-2008 Even Rouault 9 | 10 | Modifications for Zip64 support on both zip and unzip 11 | Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com ) 12 | 13 | For more info read MiniZip_info.txt 14 | 15 | --------------------------------------------------------------------------------- 16 | 17 | Condition of use and distribution are the same than zlib : 18 | 19 | This software is provided 'as-is', without any express or implied 20 | warranty. In no event will the authors be held liable for any damages 21 | arising from the use of this software. 22 | 23 | Permission is granted to anyone to use this software for any purpose, 24 | including commercial applications, and to alter it and redistribute it 25 | freely, subject to the following restrictions: 26 | 27 | 1. The origin of this software must not be misrepresented; you must not 28 | claim that you wrote the original software. If you use this software 29 | in a product, an acknowledgment in the product documentation would be 30 | appreciated but is not required. 31 | 2. Altered source versions must be plainly marked as such, and must not be 32 | misrepresented as being the original software. 33 | 3. This notice may not be removed or altered from any source distribution. 34 | 35 | --------------------------------------------------------------------------------- 36 | 37 | Changes 38 | 39 | See header of unzip64.c 40 | 41 | */ 42 | 43 | #ifndef _unz64_H 44 | #define _unz64_H 45 | 46 | #ifdef __cplusplus 47 | extern "C" { 48 | #endif 49 | 50 | #ifndef _ZLIB_H 51 | #include "zlib.h" 52 | #endif 53 | 54 | #ifndef _ZLIBIOAPI_H 55 | #include "ioapi.h" 56 | #endif 57 | 58 | #ifdef HAVE_BZIP2 59 | #include "bzlib.h" 60 | #endif 61 | 62 | #define Z_BZIP2ED 12 63 | 64 | #if defined(STRICTUNZIP) || defined(STRICTZIPUNZIP) 65 | /* like the STRICT of WIN32, we define a pointer that cannot be converted 66 | from (void*) without cast */ 67 | typedef struct TagunzFile__ { int unused; } unzFile__; 68 | typedef unzFile__ *unzFile; 69 | #else 70 | typedef voidp unzFile; 71 | #endif 72 | 73 | 74 | #define UNZ_OK (0) 75 | #define UNZ_END_OF_LIST_OF_FILE (-100) 76 | #define UNZ_ERRNO (Z_ERRNO) 77 | #define UNZ_EOF (0) 78 | #define UNZ_PARAMERROR (-102) 79 | #define UNZ_BADZIPFILE (-103) 80 | #define UNZ_INTERNALERROR (-104) 81 | #define UNZ_CRCERROR (-105) 82 | 83 | /* tm_unz contain date/time info */ 84 | typedef struct tm_unz_s 85 | { 86 | uInt tm_sec; /* seconds after the minute - [0,59] */ 87 | uInt tm_min; /* minutes after the hour - [0,59] */ 88 | uInt tm_hour; /* hours since midnight - [0,23] */ 89 | uInt tm_mday; /* day of the month - [1,31] */ 90 | uInt tm_mon; /* months since January - [0,11] */ 91 | uInt tm_year; /* years - [1980..2044] */ 92 | } tm_unz; 93 | 94 | /* unz_global_info structure contain global data about the ZIPfile 95 | These data comes from the end of central dir */ 96 | typedef struct unz_global_info64_s 97 | { 98 | ZPOS64_T number_entry; /* total number of entries in 99 | the central dir on this disk */ 100 | uLong size_comment; /* size of the global comment of the zipfile */ 101 | } unz_global_info64; 102 | 103 | typedef struct unz_global_info_s 104 | { 105 | uLong number_entry; /* total number of entries in 106 | the central dir on this disk */ 107 | uLong size_comment; /* size of the global comment of the zipfile */ 108 | } unz_global_info; 109 | 110 | /* unz_file_info contain information about a file in the zipfile */ 111 | typedef struct unz_file_info64_s 112 | { 113 | uLong version; /* version made by 2 bytes */ 114 | uLong version_needed; /* version needed to extract 2 bytes */ 115 | uLong flag; /* general purpose bit flag 2 bytes */ 116 | uLong compression_method; /* compression method 2 bytes */ 117 | uLong dosDate; /* last mod file date in Dos fmt 4 bytes */ 118 | uLong crc; /* crc-32 4 bytes */ 119 | ZPOS64_T compressed_size; /* compressed size 8 bytes */ 120 | ZPOS64_T uncompressed_size; /* uncompressed size 8 bytes */ 121 | uLong size_filename; /* filename length 2 bytes */ 122 | uLong size_file_extra; /* extra field length 2 bytes */ 123 | uLong size_file_comment; /* file comment length 2 bytes */ 124 | 125 | uLong disk_num_start; /* disk number start 2 bytes */ 126 | uLong internal_fa; /* internal file attributes 2 bytes */ 127 | uLong external_fa; /* external file attributes 4 bytes */ 128 | 129 | tm_unz tmu_date; 130 | } unz_file_info64; 131 | 132 | typedef struct unz_file_info_s 133 | { 134 | uLong version; /* version made by 2 bytes */ 135 | uLong version_needed; /* version needed to extract 2 bytes */ 136 | uLong flag; /* general purpose bit flag 2 bytes */ 137 | uLong compression_method; /* compression method 2 bytes */ 138 | uLong dosDate; /* last mod file date in Dos fmt 4 bytes */ 139 | uLong crc; /* crc-32 4 bytes */ 140 | uLong compressed_size; /* compressed size 4 bytes */ 141 | uLong uncompressed_size; /* uncompressed size 4 bytes */ 142 | uLong size_filename; /* filename length 2 bytes */ 143 | uLong size_file_extra; /* extra field length 2 bytes */ 144 | uLong size_file_comment; /* file comment length 2 bytes */ 145 | 146 | uLong disk_num_start; /* disk number start 2 bytes */ 147 | uLong internal_fa; /* internal file attributes 2 bytes */ 148 | uLong external_fa; /* external file attributes 4 bytes */ 149 | 150 | tm_unz tmu_date; 151 | } unz_file_info; 152 | 153 | extern int ZEXPORT unzStringFileNameCompare OF ((const char* fileName1, 154 | const char* fileName2, 155 | int iCaseSensitivity)); 156 | /* 157 | Compare two filename (fileName1,fileName2). 158 | If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp) 159 | If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi 160 | or strcasecmp) 161 | If iCaseSenisivity = 0, case sensitivity is defaut of your operating system 162 | (like 1 on Unix, 2 on Windows) 163 | */ 164 | 165 | 166 | extern unzFile ZEXPORT unzOpen OF((const char *path)); 167 | extern unzFile ZEXPORT unzOpen64 OF((const void *path)); 168 | /* 169 | Open a Zip file. path contain the full pathname (by example, 170 | on a Windows XP computer "c:\\zlib\\zlib113.zip" or on an Unix computer 171 | "zlib/zlib113.zip". 172 | If the zipfile cannot be opened (file don't exist or in not valid), the 173 | return value is NULL. 174 | Else, the return value is a unzFile Handle, usable with other function 175 | of this unzip package. 176 | the "64" function take a const void* pointer, because the path is just the 177 | value passed to the open64_file_func callback. 178 | Under Windows, if UNICODE is defined, using fill_fopen64_filefunc, the path 179 | is a pointer to a wide unicode string (LPCTSTR is LPCWSTR), so const char* 180 | does not describe the reality 181 | */ 182 | 183 | 184 | extern unzFile ZEXPORT unzOpen2 OF((const char *path, 185 | zlib_filefunc_def* pzlib_filefunc_def)); 186 | /* 187 | Open a Zip file, like unzOpen, but provide a set of file low level API 188 | for read/write the zip file (see ioapi.h) 189 | */ 190 | 191 | extern unzFile ZEXPORT unzOpen2_64 OF((const void *path, 192 | zlib_filefunc64_def* pzlib_filefunc_def)); 193 | /* 194 | Open a Zip file, like unz64Open, but provide a set of file low level API 195 | for read/write the zip file (see ioapi.h) 196 | */ 197 | 198 | extern int ZEXPORT unzClose OF((unzFile file)); 199 | /* 200 | Close a ZipFile opened with unzipOpen. 201 | If there is files inside the .Zip opened with unzOpenCurrentFile (see later), 202 | these files MUST be closed with unzipCloseCurrentFile before call unzipClose. 203 | return UNZ_OK if there is no problem. */ 204 | 205 | extern int ZEXPORT unzGetGlobalInfo OF((unzFile file, 206 | unz_global_info *pglobal_info)); 207 | 208 | extern int ZEXPORT unzGetGlobalInfo64 OF((unzFile file, 209 | unz_global_info64 *pglobal_info)); 210 | /* 211 | Write info about the ZipFile in the *pglobal_info structure. 212 | No preparation of the structure is needed 213 | return UNZ_OK if there is no problem. */ 214 | 215 | 216 | extern int ZEXPORT unzGetGlobalComment OF((unzFile file, 217 | char *szComment, 218 | uLong uSizeBuf)); 219 | /* 220 | Get the global comment string of the ZipFile, in the szComment buffer. 221 | uSizeBuf is the size of the szComment buffer. 222 | return the number of byte copied or an error code <0 223 | */ 224 | 225 | 226 | /***************************************************************************/ 227 | /* Unzip package allow you browse the directory of the zipfile */ 228 | 229 | extern int ZEXPORT unzGoToFirstFile OF((unzFile file)); 230 | /* 231 | Set the current file of the zipfile to the first file. 232 | return UNZ_OK if there is no problem 233 | */ 234 | 235 | extern int ZEXPORT unzGoToNextFile OF((unzFile file)); 236 | /* 237 | Set the current file of the zipfile to the next file. 238 | return UNZ_OK if there is no problem 239 | return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest. 240 | */ 241 | 242 | extern int ZEXPORT unzLocateFile OF((unzFile file, 243 | const char *szFileName, 244 | int iCaseSensitivity)); 245 | /* 246 | Try locate the file szFileName in the zipfile. 247 | For the iCaseSensitivity signification, see unzStringFileNameCompare 248 | 249 | return value : 250 | UNZ_OK if the file is found. It becomes the current file. 251 | UNZ_END_OF_LIST_OF_FILE if the file is not found 252 | */ 253 | 254 | 255 | /* ****************************************** */ 256 | /* Ryan supplied functions */ 257 | /* unz_file_info contain information about a file in the zipfile */ 258 | typedef struct unz_file_pos_s 259 | { 260 | uLong pos_in_zip_directory; /* offset in zip file directory */ 261 | uLong num_of_file; /* # of file */ 262 | } unz_file_pos; 263 | 264 | extern int ZEXPORT unzGetFilePos( 265 | unzFile file, 266 | unz_file_pos* file_pos); 267 | 268 | extern int ZEXPORT unzGoToFilePos( 269 | unzFile file, 270 | unz_file_pos* file_pos); 271 | 272 | typedef struct unz64_file_pos_s 273 | { 274 | ZPOS64_T pos_in_zip_directory; /* offset in zip file directory */ 275 | ZPOS64_T num_of_file; /* # of file */ 276 | } unz64_file_pos; 277 | 278 | extern int ZEXPORT unzGetFilePos64( 279 | unzFile file, 280 | unz64_file_pos* file_pos); 281 | 282 | extern int ZEXPORT unzGoToFilePos64( 283 | unzFile file, 284 | const unz64_file_pos* file_pos); 285 | 286 | /* ****************************************** */ 287 | 288 | extern int ZEXPORT unzGetCurrentFileInfo64 OF((unzFile file, 289 | unz_file_info64 *pfile_info, 290 | char *szFileName, 291 | uLong fileNameBufferSize, 292 | void *extraField, 293 | uLong extraFieldBufferSize, 294 | char *szComment, 295 | uLong commentBufferSize)); 296 | 297 | extern int ZEXPORT unzGetCurrentFileInfo OF((unzFile file, 298 | unz_file_info *pfile_info, 299 | char *szFileName, 300 | uLong fileNameBufferSize, 301 | void *extraField, 302 | uLong extraFieldBufferSize, 303 | char *szComment, 304 | uLong commentBufferSize)); 305 | /* 306 | Get Info about the current file 307 | if pfile_info!=NULL, the *pfile_info structure will contain somes info about 308 | the current file 309 | if szFileName!=NULL, the filemane string will be copied in szFileName 310 | (fileNameBufferSize is the size of the buffer) 311 | if extraField!=NULL, the extra field information will be copied in extraField 312 | (extraFieldBufferSize is the size of the buffer). 313 | This is the Central-header version of the extra field 314 | if szComment!=NULL, the comment string of the file will be copied in szComment 315 | (commentBufferSize is the size of the buffer) 316 | */ 317 | 318 | 319 | /** Addition for GDAL : START */ 320 | 321 | extern ZPOS64_T ZEXPORT unzGetCurrentFileZStreamPos64 OF((unzFile file)); 322 | 323 | /** Addition for GDAL : END */ 324 | 325 | 326 | /***************************************************************************/ 327 | /* for reading the content of the current zipfile, you can open it, read data 328 | from it, and close it (you can close it before reading all the file) 329 | */ 330 | 331 | extern int ZEXPORT unzOpenCurrentFile OF((unzFile file)); 332 | /* 333 | Open for reading data the current file in the zipfile. 334 | If there is no error, the return value is UNZ_OK. 335 | */ 336 | 337 | extern int ZEXPORT unzOpenCurrentFilePassword OF((unzFile file, 338 | const char* password)); 339 | /* 340 | Open for reading data the current file in the zipfile. 341 | password is a crypting password 342 | If there is no error, the return value is UNZ_OK. 343 | */ 344 | 345 | extern int ZEXPORT unzOpenCurrentFile2 OF((unzFile file, 346 | int* method, 347 | int* level, 348 | int raw)); 349 | /* 350 | Same than unzOpenCurrentFile, but open for read raw the file (not uncompress) 351 | if raw==1 352 | *method will receive method of compression, *level will receive level of 353 | compression 354 | note : you can set level parameter as NULL (if you did not want known level, 355 | but you CANNOT set method parameter as NULL 356 | */ 357 | 358 | extern int ZEXPORT unzOpenCurrentFile3 OF((unzFile file, 359 | int* method, 360 | int* level, 361 | int raw, 362 | const char* password)); 363 | /* 364 | Same than unzOpenCurrentFile, but open for read raw the file (not uncompress) 365 | if raw==1 366 | *method will receive method of compression, *level will receive level of 367 | compression 368 | note : you can set level parameter as NULL (if you did not want known level, 369 | but you CANNOT set method parameter as NULL 370 | */ 371 | 372 | 373 | extern int ZEXPORT unzCloseCurrentFile OF((unzFile file)); 374 | /* 375 | Close the file in zip opened with unzOpenCurrentFile 376 | Return UNZ_CRCERROR if all the file was read but the CRC is not good 377 | */ 378 | 379 | extern int ZEXPORT unzReadCurrentFile OF((unzFile file, 380 | voidp buf, 381 | unsigned len)); 382 | /* 383 | Read bytes from the current file (opened by unzOpenCurrentFile) 384 | buf contain buffer where data must be copied 385 | len the size of buf. 386 | 387 | return the number of byte copied if somes bytes are copied 388 | return 0 if the end of file was reached 389 | return <0 with error code if there is an error 390 | (UNZ_ERRNO for IO error, or zLib error for uncompress error) 391 | */ 392 | 393 | extern z_off_t ZEXPORT unztell OF((unzFile file)); 394 | 395 | extern ZPOS64_T ZEXPORT unztell64 OF((unzFile file)); 396 | /* 397 | Give the current position in uncompressed data 398 | */ 399 | 400 | extern int ZEXPORT unzeof OF((unzFile file)); 401 | /* 402 | return 1 if the end of file was reached, 0 elsewhere 403 | */ 404 | 405 | extern int ZEXPORT unzGetLocalExtrafield OF((unzFile file, 406 | voidp buf, 407 | unsigned len)); 408 | /* 409 | Read extra field from the current file (opened by unzOpenCurrentFile) 410 | This is the local-header version of the extra field (sometimes, there is 411 | more info in the local-header version than in the central-header) 412 | 413 | if buf==NULL, it return the size of the local extra field 414 | 415 | if buf!=NULL, len is the size of the buffer, the extra header is copied in 416 | buf. 417 | the return value is the number of bytes copied in buf, or (if <0) 418 | the error code 419 | */ 420 | 421 | /***************************************************************************/ 422 | 423 | /* Get the current file offset */ 424 | extern ZPOS64_T ZEXPORT unzGetOffset64 (unzFile file); 425 | extern uLong ZEXPORT unzGetOffset (unzFile file); 426 | 427 | /* Set the current file offset */ 428 | extern int ZEXPORT unzSetOffset64 (unzFile file, ZPOS64_T pos); 429 | extern int ZEXPORT unzSetOffset (unzFile file, uLong pos); 430 | 431 | 432 | 433 | #ifdef __cplusplus 434 | } 435 | #endif 436 | 437 | #endif /* _unz64_H */ 438 | -------------------------------------------------------------------------------- /Source/ZipPlatformFile/Private/minizip/zip.c: -------------------------------------------------------------------------------- 1 | /* zip.c -- IO on .zip files using zlib 2 | Version 1.1, February 14h, 2010 3 | part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html ) 4 | 5 | Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html ) 6 | 7 | Modifications for Zip64 support 8 | Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com ) 9 | 10 | For more info read MiniZip_info.txt 11 | 12 | Changes 13 | Oct-2009 - Mathias Svensson - Remove old C style function prototypes 14 | Oct-2009 - Mathias Svensson - Added Zip64 Support when creating new file archives 15 | Oct-2009 - Mathias Svensson - Did some code cleanup and refactoring to get better overview of some functions. 16 | Oct-2009 - Mathias Svensson - Added zipRemoveExtraInfoBlock to strip extra field data from its ZIP64 data 17 | It is used when recreting zip archive with RAW when deleting items from a zip. 18 | ZIP64 data is automaticly added to items that needs it, and existing ZIP64 data need to be removed. 19 | Oct-2009 - Mathias Svensson - Added support for BZIP2 as compression mode (bzip2 lib is required) 20 | Jan-2010 - back to unzip and minizip 1.0 name scheme, with compatibility layer 21 | 22 | */ 23 | 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include "zlib.h" 30 | #include "zip.h" 31 | 32 | #ifdef STDC 33 | # include 34 | # include 35 | # include 36 | #endif 37 | #ifdef NO_ERRNO_H 38 | extern int errno; 39 | #else 40 | # include 41 | #endif 42 | 43 | 44 | #ifndef local 45 | # define local static 46 | #endif 47 | /* compile with -Dlocal if your debugger can't find static symbols */ 48 | 49 | #ifndef VERSIONMADEBY 50 | # define VERSIONMADEBY (0x0) /* platform depedent */ 51 | #endif 52 | 53 | #ifndef Z_BUFSIZE 54 | #define Z_BUFSIZE (64*1024) //(16384) 55 | #endif 56 | 57 | #ifndef Z_MAXFILENAMEINZIP 58 | #define Z_MAXFILENAMEINZIP (256) 59 | #endif 60 | 61 | #ifndef ALLOC 62 | # define ALLOC(size) (malloc(size)) 63 | #endif 64 | #ifndef TRYFREE 65 | # define TRYFREE(p) {if (p) free(p);} 66 | #endif 67 | 68 | /* 69 | #define SIZECENTRALDIRITEM (0x2e) 70 | #define SIZEZIPLOCALHEADER (0x1e) 71 | */ 72 | 73 | /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */ 74 | 75 | 76 | // NOT sure that this work on ALL platform 77 | #define MAKEULONG64(a, b) ((ZPOS64_T)(((unsigned long)(a)) | ((ZPOS64_T)((unsigned long)(b))) << 32)) 78 | 79 | #ifndef SEEK_CUR 80 | #define SEEK_CUR 1 81 | #endif 82 | 83 | #ifndef SEEK_END 84 | #define SEEK_END 2 85 | #endif 86 | 87 | #ifndef SEEK_SET 88 | #define SEEK_SET 0 89 | #endif 90 | 91 | #ifndef DEF_MEM_LEVEL 92 | #if MAX_MEM_LEVEL >= 8 93 | # define DEF_MEM_LEVEL 8 94 | #else 95 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL 96 | #endif 97 | #endif 98 | const char zip_copyright[] =" zip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll"; 99 | 100 | 101 | #define SIZEDATA_INDATABLOCK (4096-(4*4)) 102 | 103 | #define LOCALHEADERMAGIC (0x04034b50) 104 | #define CENTRALHEADERMAGIC (0x02014b50) 105 | #define ENDHEADERMAGIC (0x06054b50) 106 | #define ZIP64ENDHEADERMAGIC (0x6064b50) 107 | #define ZIP64ENDLOCHEADERMAGIC (0x7064b50) 108 | 109 | #define FLAG_LOCALHEADER_OFFSET (0x06) 110 | #define CRC_LOCALHEADER_OFFSET (0x0e) 111 | 112 | #define SIZECENTRALHEADER (0x2e) /* 46 */ 113 | 114 | typedef struct linkedlist_datablock_internal_s 115 | { 116 | struct linkedlist_datablock_internal_s* next_datablock; 117 | uLong avail_in_this_block; 118 | uLong filled_in_this_block; 119 | uLong unused; /* for future use and alignement */ 120 | unsigned char data[SIZEDATA_INDATABLOCK]; 121 | } linkedlist_datablock_internal; 122 | 123 | typedef struct linkedlist_data_s 124 | { 125 | linkedlist_datablock_internal* first_block; 126 | linkedlist_datablock_internal* last_block; 127 | } linkedlist_data; 128 | 129 | 130 | typedef struct 131 | { 132 | z_stream stream; /* zLib stream structure for inflate */ 133 | #ifdef HAVE_BZIP2 134 | bz_stream bstream; /* bzLib stream structure for bziped */ 135 | #endif 136 | 137 | int stream_initialised; /* 1 is stream is initialised */ 138 | uInt pos_in_buffered_data; /* last written byte in buffered_data */ 139 | 140 | ZPOS64_T pos_local_header; /* offset of the local header of the file 141 | currenty writing */ 142 | char* central_header; /* central header data for the current file */ 143 | uLong size_centralExtra; 144 | uLong size_centralheader; /* size of the central header for cur file */ 145 | uLong size_centralExtraFree; /* Extra bytes allocated to the centralheader but that are not used */ 146 | uLong flag; /* flag of the file currently writing */ 147 | 148 | int method; /* compression method of file currenty wr.*/ 149 | int raw; /* 1 for directly writing raw data */ 150 | Byte buffered_data[Z_BUFSIZE];/* buffer contain compressed data to be writ*/ 151 | uLong dosDate; 152 | uLong crc32; 153 | int encrypt; 154 | int zip64; /* Add ZIP64 extened information in the extra field */ 155 | ZPOS64_T pos_zip64extrainfo; 156 | ZPOS64_T totalCompressedData; 157 | ZPOS64_T totalUncompressedData; 158 | #ifndef NOCRYPT 159 | unsigned long keys[3]; /* keys defining the pseudo-random sequence */ 160 | const z_crc_t* pcrc_32_tab; 161 | int crypt_header_size; 162 | #endif 163 | } curfile64_info; 164 | 165 | typedef struct 166 | { 167 | zlib_filefunc64_32_def z_filefunc; 168 | voidpf filestream; /* io structore of the zipfile */ 169 | linkedlist_data central_dir;/* datablock with central dir in construction*/ 170 | int in_opened_file_inzip; /* 1 if a file in the zip is currently writ.*/ 171 | curfile64_info ci; /* info on the file curretly writing */ 172 | 173 | ZPOS64_T begin_pos; /* position of the beginning of the zipfile */ 174 | ZPOS64_T add_position_when_writting_offset; 175 | ZPOS64_T number_entry; 176 | 177 | #ifndef NO_ADDFILEINEXISTINGZIP 178 | char *globalcomment; 179 | #endif 180 | 181 | } zip64_internal; 182 | 183 | 184 | #ifndef NOCRYPT 185 | #define INCLUDECRYPTINGCODE_IFCRYPTALLOWED 186 | #include "crypt.h" 187 | #endif 188 | 189 | local linkedlist_datablock_internal* allocate_new_datablock() 190 | { 191 | linkedlist_datablock_internal* ldi; 192 | ldi = (linkedlist_datablock_internal*) 193 | ALLOC(sizeof(linkedlist_datablock_internal)); 194 | if (ldi!=NULL) 195 | { 196 | ldi->next_datablock = NULL ; 197 | ldi->filled_in_this_block = 0 ; 198 | ldi->avail_in_this_block = SIZEDATA_INDATABLOCK ; 199 | } 200 | return ldi; 201 | } 202 | 203 | local void free_datablock(linkedlist_datablock_internal* ldi) 204 | { 205 | while (ldi!=NULL) 206 | { 207 | linkedlist_datablock_internal* ldinext = ldi->next_datablock; 208 | TRYFREE(ldi); 209 | ldi = ldinext; 210 | } 211 | } 212 | 213 | local void init_linkedlist(linkedlist_data* ll) 214 | { 215 | ll->first_block = ll->last_block = NULL; 216 | } 217 | 218 | local void free_linkedlist(linkedlist_data* ll) 219 | { 220 | free_datablock(ll->first_block); 221 | ll->first_block = ll->last_block = NULL; 222 | } 223 | 224 | 225 | local int add_data_in_datablock(linkedlist_data* ll, const void* buf, uLong len) 226 | { 227 | linkedlist_datablock_internal* ldi; 228 | const unsigned char* from_copy; 229 | 230 | if (ll==NULL) 231 | return ZIP_INTERNALERROR; 232 | 233 | if (ll->last_block == NULL) 234 | { 235 | ll->first_block = ll->last_block = allocate_new_datablock(); 236 | if (ll->first_block == NULL) 237 | return ZIP_INTERNALERROR; 238 | } 239 | 240 | ldi = ll->last_block; 241 | from_copy = (unsigned char*)buf; 242 | 243 | while (len>0) 244 | { 245 | uInt copy_this; 246 | uInt i; 247 | unsigned char* to_copy; 248 | 249 | if (ldi->avail_in_this_block==0) 250 | { 251 | ldi->next_datablock = allocate_new_datablock(); 252 | if (ldi->next_datablock == NULL) 253 | return ZIP_INTERNALERROR; 254 | ldi = ldi->next_datablock ; 255 | ll->last_block = ldi; 256 | } 257 | 258 | if (ldi->avail_in_this_block < len) 259 | copy_this = (uInt)ldi->avail_in_this_block; 260 | else 261 | copy_this = (uInt)len; 262 | 263 | to_copy = &(ldi->data[ldi->filled_in_this_block]); 264 | 265 | for (i=0;ifilled_in_this_block += copy_this; 269 | ldi->avail_in_this_block -= copy_this; 270 | from_copy += copy_this ; 271 | len -= copy_this; 272 | } 273 | return ZIP_OK; 274 | } 275 | 276 | 277 | 278 | /****************************************************************************/ 279 | 280 | #ifndef NO_ADDFILEINEXISTINGZIP 281 | /* =========================================================================== 282 | Inputs a long in LSB order to the given file 283 | nbByte == 1, 2 ,4 or 8 (byte, short or long, ZPOS64_T) 284 | */ 285 | 286 | local int zip64local_putValue OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T x, int nbByte)); 287 | local int zip64local_putValue (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T x, int nbByte) 288 | { 289 | unsigned char buf[8]; 290 | int n; 291 | for (n = 0; n < nbByte; n++) 292 | { 293 | buf[n] = (unsigned char)(x & 0xff); 294 | x >>= 8; 295 | } 296 | if (x != 0) 297 | { /* data overflow - hack for ZIP64 (X Roche) */ 298 | for (n = 0; n < nbByte; n++) 299 | { 300 | buf[n] = 0xff; 301 | } 302 | } 303 | 304 | if (ZWRITE64(*pzlib_filefunc_def,filestream,buf,nbByte)!=(uLong)nbByte) 305 | return ZIP_ERRNO; 306 | else 307 | return ZIP_OK; 308 | } 309 | 310 | local void zip64local_putValue_inmemory OF((void* dest, ZPOS64_T x, int nbByte)); 311 | local void zip64local_putValue_inmemory (void* dest, ZPOS64_T x, int nbByte) 312 | { 313 | unsigned char* buf=(unsigned char*)dest; 314 | int n; 315 | for (n = 0; n < nbByte; n++) { 316 | buf[n] = (unsigned char)(x & 0xff); 317 | x >>= 8; 318 | } 319 | 320 | if (x != 0) 321 | { /* data overflow - hack for ZIP64 */ 322 | for (n = 0; n < nbByte; n++) 323 | { 324 | buf[n] = 0xff; 325 | } 326 | } 327 | } 328 | 329 | /****************************************************************************/ 330 | 331 | 332 | local uLong zip64local_TmzDateToDosDate(const tm_zip* ptm) 333 | { 334 | uLong year = (uLong)ptm->tm_year; 335 | if (year>=1980) 336 | year-=1980; 337 | else if (year>=80) 338 | year-=80; 339 | return 340 | (uLong) (((ptm->tm_mday) + (32 * (ptm->tm_mon+1)) + (512 * year)) << 16) | 341 | ((ptm->tm_sec/2) + (32* ptm->tm_min) + (2048 * (uLong)ptm->tm_hour)); 342 | } 343 | 344 | 345 | /****************************************************************************/ 346 | 347 | local int zip64local_getByte OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, int *pi)); 348 | 349 | local int zip64local_getByte(const zlib_filefunc64_32_def* pzlib_filefunc_def,voidpf filestream,int* pi) 350 | { 351 | unsigned char c; 352 | int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,&c,1); 353 | if (err==1) 354 | { 355 | *pi = (int)c; 356 | return ZIP_OK; 357 | } 358 | else 359 | { 360 | if (ZERROR64(*pzlib_filefunc_def,filestream)) 361 | return ZIP_ERRNO; 362 | else 363 | return ZIP_EOF; 364 | } 365 | } 366 | 367 | 368 | /* =========================================================================== 369 | Reads a long in LSB order from the given gz_stream. Sets 370 | */ 371 | local int zip64local_getShort OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong *pX)); 372 | 373 | local int zip64local_getShort (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong* pX) 374 | { 375 | uLong x ; 376 | int i = 0; 377 | int err; 378 | 379 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 380 | x = (uLong)i; 381 | 382 | if (err==ZIP_OK) 383 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 384 | x += ((uLong)i)<<8; 385 | 386 | if (err==ZIP_OK) 387 | *pX = x; 388 | else 389 | *pX = 0; 390 | return err; 391 | } 392 | 393 | local int zip64local_getLong OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong *pX)); 394 | 395 | local int zip64local_getLong (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong* pX) 396 | { 397 | uLong x ; 398 | int i = 0; 399 | int err; 400 | 401 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 402 | x = (uLong)i; 403 | 404 | if (err==ZIP_OK) 405 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 406 | x += ((uLong)i)<<8; 407 | 408 | if (err==ZIP_OK) 409 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 410 | x += ((uLong)i)<<16; 411 | 412 | if (err==ZIP_OK) 413 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 414 | x += ((uLong)i)<<24; 415 | 416 | if (err==ZIP_OK) 417 | *pX = x; 418 | else 419 | *pX = 0; 420 | return err; 421 | } 422 | 423 | local int zip64local_getLong64 OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX)); 424 | 425 | 426 | local int zip64local_getLong64 (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX) 427 | { 428 | ZPOS64_T x; 429 | int i = 0; 430 | int err; 431 | 432 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 433 | x = (ZPOS64_T)i; 434 | 435 | if (err==ZIP_OK) 436 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 437 | x += ((ZPOS64_T)i)<<8; 438 | 439 | if (err==ZIP_OK) 440 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 441 | x += ((ZPOS64_T)i)<<16; 442 | 443 | if (err==ZIP_OK) 444 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 445 | x += ((ZPOS64_T)i)<<24; 446 | 447 | if (err==ZIP_OK) 448 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 449 | x += ((ZPOS64_T)i)<<32; 450 | 451 | if (err==ZIP_OK) 452 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 453 | x += ((ZPOS64_T)i)<<40; 454 | 455 | if (err==ZIP_OK) 456 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 457 | x += ((ZPOS64_T)i)<<48; 458 | 459 | if (err==ZIP_OK) 460 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 461 | x += ((ZPOS64_T)i)<<56; 462 | 463 | if (err==ZIP_OK) 464 | *pX = x; 465 | else 466 | *pX = 0; 467 | 468 | return err; 469 | } 470 | 471 | #ifndef BUFREADCOMMENT 472 | #define BUFREADCOMMENT (0x400) 473 | #endif 474 | /* 475 | Locate the Central directory of a zipfile (at the end, just before 476 | the global comment) 477 | */ 478 | local ZPOS64_T zip64local_SearchCentralDir OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream)); 479 | 480 | local ZPOS64_T zip64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream) 481 | { 482 | unsigned char* buf; 483 | ZPOS64_T uSizeFile; 484 | ZPOS64_T uBackRead; 485 | ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */ 486 | ZPOS64_T uPosFound=0; 487 | 488 | if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0) 489 | return 0; 490 | 491 | 492 | uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream); 493 | 494 | if (uMaxBack>uSizeFile) 495 | uMaxBack = uSizeFile; 496 | 497 | buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4); 498 | if (buf==NULL) 499 | return 0; 500 | 501 | uBackRead = 4; 502 | while (uBackReaduMaxBack) 508 | uBackRead = uMaxBack; 509 | else 510 | uBackRead+=BUFREADCOMMENT; 511 | uReadPos = uSizeFile-uBackRead ; 512 | 513 | uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? 514 | (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos); 515 | if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0) 516 | break; 517 | 518 | if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize) 519 | break; 520 | 521 | for (i=(int)uReadSize-3; (i--)>0;) 522 | if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && 523 | ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06)) 524 | { 525 | uPosFound = uReadPos+i; 526 | break; 527 | } 528 | 529 | if (uPosFound!=0) 530 | break; 531 | } 532 | TRYFREE(buf); 533 | return uPosFound; 534 | } 535 | 536 | /* 537 | Locate the End of Zip64 Central directory locator and from there find the CD of a zipfile (at the end, just before 538 | the global comment) 539 | */ 540 | local ZPOS64_T zip64local_SearchCentralDir64 OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream)); 541 | 542 | local ZPOS64_T zip64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream) 543 | { 544 | unsigned char* buf; 545 | ZPOS64_T uSizeFile; 546 | ZPOS64_T uBackRead; 547 | ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */ 548 | ZPOS64_T uPosFound=0; 549 | uLong uL; 550 | ZPOS64_T relativeOffset; 551 | 552 | if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0) 553 | return 0; 554 | 555 | uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream); 556 | 557 | if (uMaxBack>uSizeFile) 558 | uMaxBack = uSizeFile; 559 | 560 | buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4); 561 | if (buf==NULL) 562 | return 0; 563 | 564 | uBackRead = 4; 565 | while (uBackReaduMaxBack) 571 | uBackRead = uMaxBack; 572 | else 573 | uBackRead+=BUFREADCOMMENT; 574 | uReadPos = uSizeFile-uBackRead ; 575 | 576 | uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? 577 | (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos); 578 | if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0) 579 | break; 580 | 581 | if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize) 582 | break; 583 | 584 | for (i=(int)uReadSize-3; (i--)>0;) 585 | { 586 | // Signature "0x07064b50" Zip64 end of central directory locater 587 | if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && ((*(buf+i+2))==0x06) && ((*(buf+i+3))==0x07)) 588 | { 589 | uPosFound = uReadPos+i; 590 | break; 591 | } 592 | } 593 | 594 | if (uPosFound!=0) 595 | break; 596 | } 597 | 598 | TRYFREE(buf); 599 | if (uPosFound == 0) 600 | return 0; 601 | 602 | /* Zip64 end of central directory locator */ 603 | if (ZSEEK64(*pzlib_filefunc_def,filestream, uPosFound,ZLIB_FILEFUNC_SEEK_SET)!=0) 604 | return 0; 605 | 606 | /* the signature, already checked */ 607 | if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK) 608 | return 0; 609 | 610 | /* number of the disk with the start of the zip64 end of central directory */ 611 | if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK) 612 | return 0; 613 | if (uL != 0) 614 | return 0; 615 | 616 | /* relative offset of the zip64 end of central directory record */ 617 | if (zip64local_getLong64(pzlib_filefunc_def,filestream,&relativeOffset)!=ZIP_OK) 618 | return 0; 619 | 620 | /* total number of disks */ 621 | if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK) 622 | return 0; 623 | if (uL != 1) 624 | return 0; 625 | 626 | /* Goto Zip64 end of central directory record */ 627 | if (ZSEEK64(*pzlib_filefunc_def,filestream, relativeOffset,ZLIB_FILEFUNC_SEEK_SET)!=0) 628 | return 0; 629 | 630 | /* the signature */ 631 | if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK) 632 | return 0; 633 | 634 | if (uL != 0x06064b50) // signature of 'Zip64 end of central directory' 635 | return 0; 636 | 637 | return relativeOffset; 638 | } 639 | 640 | int LoadCentralDirectoryRecord(zip64_internal* pziinit) 641 | { 642 | int err=ZIP_OK; 643 | ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/ 644 | 645 | ZPOS64_T size_central_dir; /* size of the central directory */ 646 | ZPOS64_T offset_central_dir; /* offset of start of central directory */ 647 | ZPOS64_T central_pos; 648 | uLong uL; 649 | 650 | uLong number_disk; /* number of the current dist, used for 651 | spaning ZIP, unsupported, always 0*/ 652 | uLong number_disk_with_CD; /* number the the disk with central dir, used 653 | for spaning ZIP, unsupported, always 0*/ 654 | ZPOS64_T number_entry; 655 | ZPOS64_T number_entry_CD; /* total number of entries in 656 | the central dir 657 | (same than number_entry on nospan) */ 658 | uLong VersionMadeBy; 659 | uLong VersionNeeded; 660 | uLong size_comment; 661 | 662 | int hasZIP64Record = 0; 663 | 664 | // check first if we find a ZIP64 record 665 | central_pos = zip64local_SearchCentralDir64(&pziinit->z_filefunc,pziinit->filestream); 666 | if(central_pos > 0) 667 | { 668 | hasZIP64Record = 1; 669 | } 670 | else if(central_pos == 0) 671 | { 672 | central_pos = zip64local_SearchCentralDir(&pziinit->z_filefunc,pziinit->filestream); 673 | } 674 | 675 | /* disable to allow appending to empty ZIP archive 676 | if (central_pos==0) 677 | err=ZIP_ERRNO; 678 | */ 679 | 680 | if(hasZIP64Record) 681 | { 682 | ZPOS64_T sizeEndOfCentralDirectory; 683 | if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, central_pos, ZLIB_FILEFUNC_SEEK_SET) != 0) 684 | err=ZIP_ERRNO; 685 | 686 | /* the signature, already checked */ 687 | if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&uL)!=ZIP_OK) 688 | err=ZIP_ERRNO; 689 | 690 | /* size of zip64 end of central directory record */ 691 | if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream, &sizeEndOfCentralDirectory)!=ZIP_OK) 692 | err=ZIP_ERRNO; 693 | 694 | /* version made by */ 695 | if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &VersionMadeBy)!=ZIP_OK) 696 | err=ZIP_ERRNO; 697 | 698 | /* version needed to extract */ 699 | if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &VersionNeeded)!=ZIP_OK) 700 | err=ZIP_ERRNO; 701 | 702 | /* number of this disk */ 703 | if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&number_disk)!=ZIP_OK) 704 | err=ZIP_ERRNO; 705 | 706 | /* number of the disk with the start of the central directory */ 707 | if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&number_disk_with_CD)!=ZIP_OK) 708 | err=ZIP_ERRNO; 709 | 710 | /* total number of entries in the central directory on this disk */ 711 | if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream, &number_entry)!=ZIP_OK) 712 | err=ZIP_ERRNO; 713 | 714 | /* total number of entries in the central directory */ 715 | if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&number_entry_CD)!=ZIP_OK) 716 | err=ZIP_ERRNO; 717 | 718 | if ((number_entry_CD!=number_entry) || (number_disk_with_CD!=0) || (number_disk!=0)) 719 | err=ZIP_BADZIPFILE; 720 | 721 | /* size of the central directory */ 722 | if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&size_central_dir)!=ZIP_OK) 723 | err=ZIP_ERRNO; 724 | 725 | /* offset of start of central directory with respect to the 726 | starting disk number */ 727 | if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&offset_central_dir)!=ZIP_OK) 728 | err=ZIP_ERRNO; 729 | 730 | // TODO.. 731 | // read the comment from the standard central header. 732 | size_comment = 0; 733 | } 734 | else 735 | { 736 | // Read End of central Directory info 737 | if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0) 738 | err=ZIP_ERRNO; 739 | 740 | /* the signature, already checked */ 741 | if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&uL)!=ZIP_OK) 742 | err=ZIP_ERRNO; 743 | 744 | /* number of this disk */ 745 | if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream,&number_disk)!=ZIP_OK) 746 | err=ZIP_ERRNO; 747 | 748 | /* number of the disk with the start of the central directory */ 749 | if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream,&number_disk_with_CD)!=ZIP_OK) 750 | err=ZIP_ERRNO; 751 | 752 | /* total number of entries in the central dir on this disk */ 753 | number_entry = 0; 754 | if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK) 755 | err=ZIP_ERRNO; 756 | else 757 | number_entry = uL; 758 | 759 | /* total number of entries in the central dir */ 760 | number_entry_CD = 0; 761 | if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK) 762 | err=ZIP_ERRNO; 763 | else 764 | number_entry_CD = uL; 765 | 766 | if ((number_entry_CD!=number_entry) || (number_disk_with_CD!=0) || (number_disk!=0)) 767 | err=ZIP_BADZIPFILE; 768 | 769 | /* size of the central directory */ 770 | size_central_dir = 0; 771 | if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK) 772 | err=ZIP_ERRNO; 773 | else 774 | size_central_dir = uL; 775 | 776 | /* offset of start of central directory with respect to the starting disk number */ 777 | offset_central_dir = 0; 778 | if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK) 779 | err=ZIP_ERRNO; 780 | else 781 | offset_central_dir = uL; 782 | 783 | 784 | /* zipfile global comment length */ 785 | if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &size_comment)!=ZIP_OK) 786 | err=ZIP_ERRNO; 787 | } 788 | 789 | if ((central_posz_filefunc, pziinit->filestream); 796 | return ZIP_ERRNO; 797 | } 798 | 799 | if (size_comment>0) 800 | { 801 | pziinit->globalcomment = (char*)ALLOC(size_comment+1); 802 | if (pziinit->globalcomment) 803 | { 804 | size_comment = ZREAD64(pziinit->z_filefunc, pziinit->filestream, pziinit->globalcomment,size_comment); 805 | pziinit->globalcomment[size_comment]=0; 806 | } 807 | } 808 | 809 | byte_before_the_zipfile = central_pos - (offset_central_dir+size_central_dir); 810 | pziinit->add_position_when_writting_offset = byte_before_the_zipfile; 811 | 812 | { 813 | ZPOS64_T size_central_dir_to_read = size_central_dir; 814 | size_t buf_size = SIZEDATA_INDATABLOCK; 815 | void* buf_read = (void*)ALLOC(buf_size); 816 | if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, offset_central_dir + byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0) 817 | err=ZIP_ERRNO; 818 | 819 | while ((size_central_dir_to_read>0) && (err==ZIP_OK)) 820 | { 821 | ZPOS64_T read_this = SIZEDATA_INDATABLOCK; 822 | if (read_this > size_central_dir_to_read) 823 | read_this = size_central_dir_to_read; 824 | 825 | if (ZREAD64(pziinit->z_filefunc, pziinit->filestream,buf_read,(uLong)read_this) != read_this) 826 | err=ZIP_ERRNO; 827 | 828 | if (err==ZIP_OK) 829 | err = add_data_in_datablock(&pziinit->central_dir,buf_read, (uLong)read_this); 830 | 831 | size_central_dir_to_read-=read_this; 832 | } 833 | TRYFREE(buf_read); 834 | } 835 | pziinit->begin_pos = byte_before_the_zipfile; 836 | pziinit->number_entry = number_entry_CD; 837 | 838 | if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, offset_central_dir+byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET) != 0) 839 | err=ZIP_ERRNO; 840 | 841 | return err; 842 | } 843 | 844 | 845 | #endif /* !NO_ADDFILEINEXISTINGZIP*/ 846 | 847 | 848 | /************************************************************/ 849 | extern zipFile ZEXPORT zipOpen3 (const void *pathname, int append, zipcharpc* globalcomment, zlib_filefunc64_32_def* pzlib_filefunc64_32_def) 850 | { 851 | zip64_internal ziinit; 852 | zip64_internal* zi; 853 | int err=ZIP_OK; 854 | 855 | ziinit.z_filefunc.zseek32_file = NULL; 856 | ziinit.z_filefunc.ztell32_file = NULL; 857 | if (pzlib_filefunc64_32_def==NULL) 858 | fill_fopen64_filefunc(&ziinit.z_filefunc.zfile_func64); 859 | else 860 | ziinit.z_filefunc = *pzlib_filefunc64_32_def; 861 | 862 | ziinit.filestream = ZOPEN64(ziinit.z_filefunc, 863 | pathname, 864 | (append == APPEND_STATUS_CREATE) ? 865 | (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_CREATE) : 866 | (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_EXISTING)); 867 | 868 | if (ziinit.filestream == NULL) 869 | return NULL; 870 | 871 | if (append == APPEND_STATUS_CREATEAFTER) 872 | ZSEEK64(ziinit.z_filefunc,ziinit.filestream,0,SEEK_END); 873 | 874 | ziinit.begin_pos = ZTELL64(ziinit.z_filefunc,ziinit.filestream); 875 | ziinit.in_opened_file_inzip = 0; 876 | ziinit.ci.stream_initialised = 0; 877 | ziinit.number_entry = 0; 878 | ziinit.add_position_when_writting_offset = 0; 879 | init_linkedlist(&(ziinit.central_dir)); 880 | 881 | 882 | 883 | zi = (zip64_internal*)ALLOC(sizeof(zip64_internal)); 884 | if (zi==NULL) 885 | { 886 | ZCLOSE64(ziinit.z_filefunc,ziinit.filestream); 887 | return NULL; 888 | } 889 | 890 | /* now we add file in a zipfile */ 891 | # ifndef NO_ADDFILEINEXISTINGZIP 892 | ziinit.globalcomment = NULL; 893 | if (append == APPEND_STATUS_ADDINZIP) 894 | { 895 | // Read and Cache Central Directory Records 896 | err = LoadCentralDirectoryRecord(&ziinit); 897 | } 898 | 899 | if (globalcomment) 900 | { 901 | *globalcomment = ziinit.globalcomment; 902 | } 903 | # endif /* !NO_ADDFILEINEXISTINGZIP*/ 904 | 905 | if (err != ZIP_OK) 906 | { 907 | # ifndef NO_ADDFILEINEXISTINGZIP 908 | TRYFREE(ziinit.globalcomment); 909 | # endif /* !NO_ADDFILEINEXISTINGZIP*/ 910 | TRYFREE(zi); 911 | return NULL; 912 | } 913 | else 914 | { 915 | *zi = ziinit; 916 | return (zipFile)zi; 917 | } 918 | } 919 | 920 | extern zipFile ZEXPORT zipOpen2 (const char *pathname, int append, zipcharpc* globalcomment, zlib_filefunc_def* pzlib_filefunc32_def) 921 | { 922 | if (pzlib_filefunc32_def != NULL) 923 | { 924 | zlib_filefunc64_32_def zlib_filefunc64_32_def_fill; 925 | fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill,pzlib_filefunc32_def); 926 | return zipOpen3(pathname, append, globalcomment, &zlib_filefunc64_32_def_fill); 927 | } 928 | else 929 | return zipOpen3(pathname, append, globalcomment, NULL); 930 | } 931 | 932 | extern zipFile ZEXPORT zipOpen2_64 (const void *pathname, int append, zipcharpc* globalcomment, zlib_filefunc64_def* pzlib_filefunc_def) 933 | { 934 | if (pzlib_filefunc_def != NULL) 935 | { 936 | zlib_filefunc64_32_def zlib_filefunc64_32_def_fill; 937 | zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def; 938 | zlib_filefunc64_32_def_fill.ztell32_file = NULL; 939 | zlib_filefunc64_32_def_fill.zseek32_file = NULL; 940 | return zipOpen3(pathname, append, globalcomment, &zlib_filefunc64_32_def_fill); 941 | } 942 | else 943 | return zipOpen3(pathname, append, globalcomment, NULL); 944 | } 945 | 946 | 947 | 948 | extern zipFile ZEXPORT zipOpen (const char* pathname, int append) 949 | { 950 | return zipOpen3((const void*)pathname,append,NULL,NULL); 951 | } 952 | 953 | extern zipFile ZEXPORT zipOpen64 (const void* pathname, int append) 954 | { 955 | return zipOpen3(pathname,append,NULL,NULL); 956 | } 957 | 958 | int Write_LocalFileHeader(zip64_internal* zi, const char* filename, uInt size_extrafield_local, const void* extrafield_local) 959 | { 960 | /* write the local header */ 961 | int err; 962 | uInt size_filename = (uInt)strlen(filename); 963 | uInt size_extrafield = size_extrafield_local; 964 | 965 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)LOCALHEADERMAGIC, 4); 966 | 967 | if (err==ZIP_OK) 968 | { 969 | if(zi->ci.zip64) 970 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2);/* version needed to extract */ 971 | else 972 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)20,2);/* version needed to extract */ 973 | } 974 | 975 | if (err==ZIP_OK) 976 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.flag,2); 977 | 978 | if (err==ZIP_OK) 979 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.method,2); 980 | 981 | if (err==ZIP_OK) 982 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.dosDate,4); 983 | 984 | // CRC / Compressed size / Uncompressed size will be filled in later and rewritten later 985 | if (err==ZIP_OK) 986 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* crc 32, unknown */ 987 | if (err==ZIP_OK) 988 | { 989 | if(zi->ci.zip64) 990 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xFFFFFFFF,4); /* compressed size, unknown */ 991 | else 992 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* compressed size, unknown */ 993 | } 994 | if (err==ZIP_OK) 995 | { 996 | if(zi->ci.zip64) 997 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xFFFFFFFF,4); /* uncompressed size, unknown */ 998 | else 999 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* uncompressed size, unknown */ 1000 | } 1001 | 1002 | if (err==ZIP_OK) 1003 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_filename,2); 1004 | 1005 | if(zi->ci.zip64) 1006 | { 1007 | size_extrafield += 20; 1008 | } 1009 | 1010 | if (err==ZIP_OK) 1011 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_extrafield,2); 1012 | 1013 | if ((err==ZIP_OK) && (size_filename > 0)) 1014 | { 1015 | if (ZWRITE64(zi->z_filefunc,zi->filestream,filename,size_filename)!=size_filename) 1016 | err = ZIP_ERRNO; 1017 | } 1018 | 1019 | if ((err==ZIP_OK) && (size_extrafield_local > 0)) 1020 | { 1021 | if (ZWRITE64(zi->z_filefunc, zi->filestream, extrafield_local, size_extrafield_local) != size_extrafield_local) 1022 | err = ZIP_ERRNO; 1023 | } 1024 | 1025 | 1026 | if ((err==ZIP_OK) && (zi->ci.zip64)) 1027 | { 1028 | // write the Zip64 extended info 1029 | short HeaderID = 1; 1030 | short DataSize = 16; 1031 | ZPOS64_T CompressedSize = 0; 1032 | ZPOS64_T UncompressedSize = 0; 1033 | 1034 | // Remember position of Zip64 extended info for the local file header. (needed when we update size after done with file) 1035 | zi->ci.pos_zip64extrainfo = ZTELL64(zi->z_filefunc,zi->filestream); 1036 | 1037 | err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (short)HeaderID,2); 1038 | err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (short)DataSize,2); 1039 | 1040 | err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)UncompressedSize,8); 1041 | err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)CompressedSize,8); 1042 | } 1043 | 1044 | return err; 1045 | } 1046 | 1047 | /* 1048 | NOTE. 1049 | When writing RAW the ZIP64 extended information in extrafield_local and extrafield_global needs to be stripped 1050 | before calling this function it can be done with zipRemoveExtraInfoBlock 1051 | 1052 | It is not done here because then we need to realloc a new buffer since parameters are 'const' and I want to minimize 1053 | unnecessary allocations. 1054 | */ 1055 | extern int ZEXPORT zipOpenNewFileInZip4_64 (zipFile file, const char* filename, const zip_fileinfo* zipfi, 1056 | const void* extrafield_local, uInt size_extrafield_local, 1057 | const void* extrafield_global, uInt size_extrafield_global, 1058 | const char* comment, int method, int level, int raw, 1059 | int windowBits,int memLevel, int strategy, 1060 | const char* password, uLong crcForCrypting, 1061 | uLong versionMadeBy, uLong flagBase, int zip64) 1062 | { 1063 | zip64_internal* zi; 1064 | uInt size_filename; 1065 | uInt size_comment; 1066 | uInt i; 1067 | int err = ZIP_OK; 1068 | 1069 | # ifdef NOCRYPT 1070 | (crcForCrypting); 1071 | if (password != NULL) 1072 | return ZIP_PARAMERROR; 1073 | # endif 1074 | 1075 | if (file == NULL) 1076 | return ZIP_PARAMERROR; 1077 | 1078 | #ifdef HAVE_BZIP2 1079 | if ((method!=0) && (method!=Z_DEFLATED) && (method!=Z_BZIP2ED)) 1080 | return ZIP_PARAMERROR; 1081 | #else 1082 | if ((method!=0) && (method!=Z_DEFLATED)) 1083 | return ZIP_PARAMERROR; 1084 | #endif 1085 | 1086 | zi = (zip64_internal*)file; 1087 | 1088 | if (zi->in_opened_file_inzip == 1) 1089 | { 1090 | err = zipCloseFileInZip (file); 1091 | if (err != ZIP_OK) 1092 | return err; 1093 | } 1094 | 1095 | if (filename==NULL) 1096 | filename="-"; 1097 | 1098 | if (comment==NULL) 1099 | size_comment = 0; 1100 | else 1101 | size_comment = (uInt)strlen(comment); 1102 | 1103 | size_filename = (uInt)strlen(filename); 1104 | 1105 | if (zipfi == NULL) 1106 | zi->ci.dosDate = 0; 1107 | else 1108 | { 1109 | if (zipfi->dosDate != 0) 1110 | zi->ci.dosDate = zipfi->dosDate; 1111 | else 1112 | zi->ci.dosDate = zip64local_TmzDateToDosDate(&zipfi->tmz_date); 1113 | } 1114 | 1115 | zi->ci.flag = flagBase; 1116 | if ((level==8) || (level==9)) 1117 | zi->ci.flag |= 2; 1118 | if (level==2) 1119 | zi->ci.flag |= 4; 1120 | if (level==1) 1121 | zi->ci.flag |= 6; 1122 | if (password != NULL) 1123 | zi->ci.flag |= 1; 1124 | 1125 | zi->ci.crc32 = 0; 1126 | zi->ci.method = method; 1127 | zi->ci.encrypt = 0; 1128 | zi->ci.stream_initialised = 0; 1129 | zi->ci.pos_in_buffered_data = 0; 1130 | zi->ci.raw = raw; 1131 | zi->ci.pos_local_header = ZTELL64(zi->z_filefunc,zi->filestream); 1132 | 1133 | zi->ci.size_centralheader = SIZECENTRALHEADER + size_filename + size_extrafield_global + size_comment; 1134 | zi->ci.size_centralExtraFree = 32; // Extra space we have reserved in case we need to add ZIP64 extra info data 1135 | 1136 | zi->ci.central_header = (char*)ALLOC((uInt)zi->ci.size_centralheader + zi->ci.size_centralExtraFree); 1137 | 1138 | zi->ci.size_centralExtra = size_extrafield_global; 1139 | zip64local_putValue_inmemory(zi->ci.central_header,(uLong)CENTRALHEADERMAGIC,4); 1140 | /* version info */ 1141 | zip64local_putValue_inmemory(zi->ci.central_header+4,(uLong)versionMadeBy,2); 1142 | zip64local_putValue_inmemory(zi->ci.central_header+6,(uLong)20,2); 1143 | zip64local_putValue_inmemory(zi->ci.central_header+8,(uLong)zi->ci.flag,2); 1144 | zip64local_putValue_inmemory(zi->ci.central_header+10,(uLong)zi->ci.method,2); 1145 | zip64local_putValue_inmemory(zi->ci.central_header+12,(uLong)zi->ci.dosDate,4); 1146 | zip64local_putValue_inmemory(zi->ci.central_header+16,(uLong)0,4); /*crc*/ 1147 | zip64local_putValue_inmemory(zi->ci.central_header+20,(uLong)0,4); /*compr size*/ 1148 | zip64local_putValue_inmemory(zi->ci.central_header+24,(uLong)0,4); /*uncompr size*/ 1149 | zip64local_putValue_inmemory(zi->ci.central_header+28,(uLong)size_filename,2); 1150 | zip64local_putValue_inmemory(zi->ci.central_header+30,(uLong)size_extrafield_global,2); 1151 | zip64local_putValue_inmemory(zi->ci.central_header+32,(uLong)size_comment,2); 1152 | zip64local_putValue_inmemory(zi->ci.central_header+34,(uLong)0,2); /*disk nm start*/ 1153 | 1154 | if (zipfi==NULL) 1155 | zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)0,2); 1156 | else 1157 | zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)zipfi->internal_fa,2); 1158 | 1159 | if (zipfi==NULL) 1160 | zip64local_putValue_inmemory(zi->ci.central_header+38,(uLong)0,4); 1161 | else 1162 | zip64local_putValue_inmemory(zi->ci.central_header+38,(uLong)zipfi->external_fa,4); 1163 | 1164 | if(zi->ci.pos_local_header >= 0xffffffff) 1165 | zip64local_putValue_inmemory(zi->ci.central_header+42,(uLong)0xffffffff,4); 1166 | else 1167 | zip64local_putValue_inmemory(zi->ci.central_header+42,(uLong)zi->ci.pos_local_header - zi->add_position_when_writting_offset,4); 1168 | 1169 | for (i=0;ici.central_header+SIZECENTRALHEADER+i) = *(filename+i); 1171 | 1172 | for (i=0;ici.central_header+SIZECENTRALHEADER+size_filename+i) = 1174 | *(((const char*)extrafield_global)+i); 1175 | 1176 | for (i=0;ici.central_header+SIZECENTRALHEADER+size_filename+ 1178 | size_extrafield_global+i) = *(comment+i); 1179 | if (zi->ci.central_header == NULL) 1180 | return ZIP_INTERNALERROR; 1181 | 1182 | zi->ci.zip64 = zip64; 1183 | zi->ci.totalCompressedData = 0; 1184 | zi->ci.totalUncompressedData = 0; 1185 | zi->ci.pos_zip64extrainfo = 0; 1186 | 1187 | err = Write_LocalFileHeader(zi, filename, size_extrafield_local, extrafield_local); 1188 | 1189 | #ifdef HAVE_BZIP2 1190 | zi->ci.bstream.avail_in = (uInt)0; 1191 | zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE; 1192 | zi->ci.bstream.next_out = (char*)zi->ci.buffered_data; 1193 | zi->ci.bstream.total_in_hi32 = 0; 1194 | zi->ci.bstream.total_in_lo32 = 0; 1195 | zi->ci.bstream.total_out_hi32 = 0; 1196 | zi->ci.bstream.total_out_lo32 = 0; 1197 | #endif 1198 | 1199 | zi->ci.stream.avail_in = (uInt)0; 1200 | zi->ci.stream.avail_out = (uInt)Z_BUFSIZE; 1201 | zi->ci.stream.next_out = zi->ci.buffered_data; 1202 | zi->ci.stream.total_in = 0; 1203 | zi->ci.stream.total_out = 0; 1204 | zi->ci.stream.data_type = Z_BINARY; 1205 | 1206 | #ifdef HAVE_BZIP2 1207 | if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED || zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw)) 1208 | #else 1209 | if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED) && (!zi->ci.raw)) 1210 | #endif 1211 | { 1212 | if(zi->ci.method == Z_DEFLATED) 1213 | { 1214 | zi->ci.stream.zalloc = (alloc_func)0; 1215 | zi->ci.stream.zfree = (free_func)0; 1216 | zi->ci.stream.opaque = (voidpf)0; 1217 | 1218 | if (windowBits>0) 1219 | windowBits = -windowBits; 1220 | 1221 | err = deflateInit2(&zi->ci.stream, level, Z_DEFLATED, windowBits, memLevel, strategy); 1222 | 1223 | if (err==Z_OK) 1224 | zi->ci.stream_initialised = Z_DEFLATED; 1225 | } 1226 | else if(zi->ci.method == Z_BZIP2ED) 1227 | { 1228 | #ifdef HAVE_BZIP2 1229 | // Init BZip stuff here 1230 | zi->ci.bstream.bzalloc = 0; 1231 | zi->ci.bstream.bzfree = 0; 1232 | zi->ci.bstream.opaque = (voidpf)0; 1233 | 1234 | err = BZ2_bzCompressInit(&zi->ci.bstream, level, 0,35); 1235 | if(err == BZ_OK) 1236 | zi->ci.stream_initialised = Z_BZIP2ED; 1237 | #endif 1238 | } 1239 | 1240 | } 1241 | 1242 | # ifndef NOCRYPT 1243 | zi->ci.crypt_header_size = 0; 1244 | if ((err==Z_OK) && (password != NULL)) 1245 | { 1246 | unsigned char bufHead[RAND_HEAD_LEN]; 1247 | unsigned int sizeHead; 1248 | zi->ci.encrypt = 1; 1249 | zi->ci.pcrc_32_tab = get_crc_table(); 1250 | /*init_keys(password,zi->ci.keys,zi->ci.pcrc_32_tab);*/ 1251 | 1252 | sizeHead=crypthead(password,bufHead,RAND_HEAD_LEN,zi->ci.keys,zi->ci.pcrc_32_tab,crcForCrypting); 1253 | zi->ci.crypt_header_size = sizeHead; 1254 | 1255 | if (ZWRITE64(zi->z_filefunc,zi->filestream,bufHead,sizeHead) != sizeHead) 1256 | err = ZIP_ERRNO; 1257 | } 1258 | # endif 1259 | 1260 | if (err==Z_OK) 1261 | zi->in_opened_file_inzip = 1; 1262 | return err; 1263 | } 1264 | 1265 | extern int ZEXPORT zipOpenNewFileInZip4 (zipFile file, const char* filename, const zip_fileinfo* zipfi, 1266 | const void* extrafield_local, uInt size_extrafield_local, 1267 | const void* extrafield_global, uInt size_extrafield_global, 1268 | const char* comment, int method, int level, int raw, 1269 | int windowBits,int memLevel, int strategy, 1270 | const char* password, uLong crcForCrypting, 1271 | uLong versionMadeBy, uLong flagBase) 1272 | { 1273 | return zipOpenNewFileInZip4_64 (file, filename, zipfi, 1274 | extrafield_local, size_extrafield_local, 1275 | extrafield_global, size_extrafield_global, 1276 | comment, method, level, raw, 1277 | windowBits, memLevel, strategy, 1278 | password, crcForCrypting, versionMadeBy, flagBase, 0); 1279 | } 1280 | 1281 | extern int ZEXPORT zipOpenNewFileInZip3 (zipFile file, const char* filename, const zip_fileinfo* zipfi, 1282 | const void* extrafield_local, uInt size_extrafield_local, 1283 | const void* extrafield_global, uInt size_extrafield_global, 1284 | const char* comment, int method, int level, int raw, 1285 | int windowBits,int memLevel, int strategy, 1286 | const char* password, uLong crcForCrypting) 1287 | { 1288 | return zipOpenNewFileInZip4_64 (file, filename, zipfi, 1289 | extrafield_local, size_extrafield_local, 1290 | extrafield_global, size_extrafield_global, 1291 | comment, method, level, raw, 1292 | windowBits, memLevel, strategy, 1293 | password, crcForCrypting, VERSIONMADEBY, 0, 0); 1294 | } 1295 | 1296 | extern int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char* filename, const zip_fileinfo* zipfi, 1297 | const void* extrafield_local, uInt size_extrafield_local, 1298 | const void* extrafield_global, uInt size_extrafield_global, 1299 | const char* comment, int method, int level, int raw, 1300 | int windowBits,int memLevel, int strategy, 1301 | const char* password, uLong crcForCrypting, int zip64) 1302 | { 1303 | return zipOpenNewFileInZip4_64 (file, filename, zipfi, 1304 | extrafield_local, size_extrafield_local, 1305 | extrafield_global, size_extrafield_global, 1306 | comment, method, level, raw, 1307 | windowBits, memLevel, strategy, 1308 | password, crcForCrypting, VERSIONMADEBY, 0, zip64); 1309 | } 1310 | 1311 | extern int ZEXPORT zipOpenNewFileInZip2(zipFile file, const char* filename, const zip_fileinfo* zipfi, 1312 | const void* extrafield_local, uInt size_extrafield_local, 1313 | const void* extrafield_global, uInt size_extrafield_global, 1314 | const char* comment, int method, int level, int raw) 1315 | { 1316 | return zipOpenNewFileInZip4_64 (file, filename, zipfi, 1317 | extrafield_local, size_extrafield_local, 1318 | extrafield_global, size_extrafield_global, 1319 | comment, method, level, raw, 1320 | -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, 1321 | NULL, 0, VERSIONMADEBY, 0, 0); 1322 | } 1323 | 1324 | extern int ZEXPORT zipOpenNewFileInZip2_64(zipFile file, const char* filename, const zip_fileinfo* zipfi, 1325 | const void* extrafield_local, uInt size_extrafield_local, 1326 | const void* extrafield_global, uInt size_extrafield_global, 1327 | const char* comment, int method, int level, int raw, int zip64) 1328 | { 1329 | return zipOpenNewFileInZip4_64 (file, filename, zipfi, 1330 | extrafield_local, size_extrafield_local, 1331 | extrafield_global, size_extrafield_global, 1332 | comment, method, level, raw, 1333 | -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, 1334 | NULL, 0, VERSIONMADEBY, 0, zip64); 1335 | } 1336 | 1337 | extern int ZEXPORT zipOpenNewFileInZip64 (zipFile file, const char* filename, const zip_fileinfo* zipfi, 1338 | const void* extrafield_local, uInt size_extrafield_local, 1339 | const void*extrafield_global, uInt size_extrafield_global, 1340 | const char* comment, int method, int level, int zip64) 1341 | { 1342 | return zipOpenNewFileInZip4_64 (file, filename, zipfi, 1343 | extrafield_local, size_extrafield_local, 1344 | extrafield_global, size_extrafield_global, 1345 | comment, method, level, 0, 1346 | -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, 1347 | NULL, 0, VERSIONMADEBY, 0, zip64); 1348 | } 1349 | 1350 | extern int ZEXPORT zipOpenNewFileInZip (zipFile file, const char* filename, const zip_fileinfo* zipfi, 1351 | const void* extrafield_local, uInt size_extrafield_local, 1352 | const void*extrafield_global, uInt size_extrafield_global, 1353 | const char* comment, int method, int level) 1354 | { 1355 | return zipOpenNewFileInZip4_64 (file, filename, zipfi, 1356 | extrafield_local, size_extrafield_local, 1357 | extrafield_global, size_extrafield_global, 1358 | comment, method, level, 0, 1359 | -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, 1360 | NULL, 0, VERSIONMADEBY, 0, 0); 1361 | } 1362 | 1363 | local int zip64FlushWriteBuffer(zip64_internal* zi) 1364 | { 1365 | int err=ZIP_OK; 1366 | 1367 | if (zi->ci.encrypt != 0) 1368 | { 1369 | #ifndef NOCRYPT 1370 | uInt i; 1371 | int t; 1372 | for (i=0;ici.pos_in_buffered_data;i++) 1373 | zi->ci.buffered_data[i] = zencode(zi->ci.keys, zi->ci.pcrc_32_tab, zi->ci.buffered_data[i],t); 1374 | #endif 1375 | } 1376 | 1377 | if (ZWRITE64(zi->z_filefunc,zi->filestream,zi->ci.buffered_data,zi->ci.pos_in_buffered_data) != zi->ci.pos_in_buffered_data) 1378 | err = ZIP_ERRNO; 1379 | 1380 | zi->ci.totalCompressedData += zi->ci.pos_in_buffered_data; 1381 | 1382 | #ifdef HAVE_BZIP2 1383 | if(zi->ci.method == Z_BZIP2ED) 1384 | { 1385 | zi->ci.totalUncompressedData += zi->ci.bstream.total_in_lo32; 1386 | zi->ci.bstream.total_in_lo32 = 0; 1387 | zi->ci.bstream.total_in_hi32 = 0; 1388 | } 1389 | else 1390 | #endif 1391 | { 1392 | zi->ci.totalUncompressedData += zi->ci.stream.total_in; 1393 | zi->ci.stream.total_in = 0; 1394 | } 1395 | 1396 | 1397 | zi->ci.pos_in_buffered_data = 0; 1398 | 1399 | return err; 1400 | } 1401 | 1402 | extern int ZEXPORT zipWriteInFileInZip (zipFile file,const void* buf,unsigned int len) 1403 | { 1404 | zip64_internal* zi; 1405 | int err=ZIP_OK; 1406 | 1407 | if (file == NULL) 1408 | return ZIP_PARAMERROR; 1409 | zi = (zip64_internal*)file; 1410 | 1411 | if (zi->in_opened_file_inzip == 0) 1412 | return ZIP_PARAMERROR; 1413 | 1414 | zi->ci.crc32 = crc32(zi->ci.crc32,buf,(uInt)len); 1415 | 1416 | #ifdef HAVE_BZIP2 1417 | if(zi->ci.method == Z_BZIP2ED && (!zi->ci.raw)) 1418 | { 1419 | zi->ci.bstream.next_in = (void*)buf; 1420 | zi->ci.bstream.avail_in = len; 1421 | err = BZ_RUN_OK; 1422 | 1423 | while ((err==BZ_RUN_OK) && (zi->ci.bstream.avail_in>0)) 1424 | { 1425 | if (zi->ci.bstream.avail_out == 0) 1426 | { 1427 | if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO) 1428 | err = ZIP_ERRNO; 1429 | zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE; 1430 | zi->ci.bstream.next_out = (char*)zi->ci.buffered_data; 1431 | } 1432 | 1433 | 1434 | if(err != BZ_RUN_OK) 1435 | break; 1436 | 1437 | if ((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw)) 1438 | { 1439 | uLong uTotalOutBefore_lo = zi->ci.bstream.total_out_lo32; 1440 | // uLong uTotalOutBefore_hi = zi->ci.bstream.total_out_hi32; 1441 | err=BZ2_bzCompress(&zi->ci.bstream, BZ_RUN); 1442 | 1443 | zi->ci.pos_in_buffered_data += (uInt)(zi->ci.bstream.total_out_lo32 - uTotalOutBefore_lo) ; 1444 | } 1445 | } 1446 | 1447 | if(err == BZ_RUN_OK) 1448 | err = ZIP_OK; 1449 | } 1450 | else 1451 | #endif 1452 | { 1453 | zi->ci.stream.next_in = (Bytef*)buf; 1454 | zi->ci.stream.avail_in = len; 1455 | 1456 | while ((err==ZIP_OK) && (zi->ci.stream.avail_in>0)) 1457 | { 1458 | if (zi->ci.stream.avail_out == 0) 1459 | { 1460 | if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO) 1461 | err = ZIP_ERRNO; 1462 | zi->ci.stream.avail_out = (uInt)Z_BUFSIZE; 1463 | zi->ci.stream.next_out = zi->ci.buffered_data; 1464 | } 1465 | 1466 | 1467 | if(err != ZIP_OK) 1468 | break; 1469 | 1470 | if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw)) 1471 | { 1472 | uLong uTotalOutBefore = zi->ci.stream.total_out; 1473 | err=deflate(&zi->ci.stream, Z_NO_FLUSH); 1474 | if(uTotalOutBefore > zi->ci.stream.total_out) 1475 | { 1476 | int bBreak = 0; 1477 | bBreak++; 1478 | } 1479 | 1480 | zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ; 1481 | } 1482 | else 1483 | { 1484 | uInt copy_this,i; 1485 | if (zi->ci.stream.avail_in < zi->ci.stream.avail_out) 1486 | copy_this = zi->ci.stream.avail_in; 1487 | else 1488 | copy_this = zi->ci.stream.avail_out; 1489 | 1490 | for (i = 0; i < copy_this; i++) 1491 | *(((char*)zi->ci.stream.next_out)+i) = 1492 | *(((const char*)zi->ci.stream.next_in)+i); 1493 | { 1494 | zi->ci.stream.avail_in -= copy_this; 1495 | zi->ci.stream.avail_out-= copy_this; 1496 | zi->ci.stream.next_in+= copy_this; 1497 | zi->ci.stream.next_out+= copy_this; 1498 | zi->ci.stream.total_in+= copy_this; 1499 | zi->ci.stream.total_out+= copy_this; 1500 | zi->ci.pos_in_buffered_data += copy_this; 1501 | } 1502 | } 1503 | }// while(...) 1504 | } 1505 | 1506 | return err; 1507 | } 1508 | 1509 | extern int ZEXPORT zipCloseFileInZipRaw (zipFile file, uLong uncompressed_size, uLong crc32) 1510 | { 1511 | return zipCloseFileInZipRaw64 (file, uncompressed_size, crc32); 1512 | } 1513 | 1514 | extern int ZEXPORT zipCloseFileInZipRaw64 (zipFile file, ZPOS64_T uncompressed_size, uLong crc32) 1515 | { 1516 | zip64_internal* zi; 1517 | ZPOS64_T compressed_size; 1518 | uLong invalidValue = 0xffffffff; 1519 | short datasize = 0; 1520 | int err=ZIP_OK; 1521 | 1522 | if (file == NULL) 1523 | return ZIP_PARAMERROR; 1524 | zi = (zip64_internal*)file; 1525 | 1526 | if (zi->in_opened_file_inzip == 0) 1527 | return ZIP_PARAMERROR; 1528 | zi->ci.stream.avail_in = 0; 1529 | 1530 | if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw)) 1531 | { 1532 | while (err==ZIP_OK) 1533 | { 1534 | uLong uTotalOutBefore; 1535 | if (zi->ci.stream.avail_out == 0) 1536 | { 1537 | if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO) 1538 | err = ZIP_ERRNO; 1539 | zi->ci.stream.avail_out = (uInt)Z_BUFSIZE; 1540 | zi->ci.stream.next_out = zi->ci.buffered_data; 1541 | } 1542 | uTotalOutBefore = zi->ci.stream.total_out; 1543 | err=deflate(&zi->ci.stream, Z_FINISH); 1544 | zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ; 1545 | } 1546 | } 1547 | else if ((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw)) 1548 | { 1549 | #ifdef HAVE_BZIP2 1550 | err = BZ_FINISH_OK; 1551 | while (err==BZ_FINISH_OK) 1552 | { 1553 | uLong uTotalOutBefore; 1554 | if (zi->ci.bstream.avail_out == 0) 1555 | { 1556 | if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO) 1557 | err = ZIP_ERRNO; 1558 | zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE; 1559 | zi->ci.bstream.next_out = (char*)zi->ci.buffered_data; 1560 | } 1561 | uTotalOutBefore = zi->ci.bstream.total_out_lo32; 1562 | err=BZ2_bzCompress(&zi->ci.bstream, BZ_FINISH); 1563 | if(err == BZ_STREAM_END) 1564 | err = Z_STREAM_END; 1565 | 1566 | zi->ci.pos_in_buffered_data += (uInt)(zi->ci.bstream.total_out_lo32 - uTotalOutBefore); 1567 | } 1568 | 1569 | if(err == BZ_FINISH_OK) 1570 | err = ZIP_OK; 1571 | #endif 1572 | } 1573 | 1574 | if (err==Z_STREAM_END) 1575 | err=ZIP_OK; /* this is normal */ 1576 | 1577 | if ((zi->ci.pos_in_buffered_data>0) && (err==ZIP_OK)) 1578 | { 1579 | if (zip64FlushWriteBuffer(zi)==ZIP_ERRNO) 1580 | err = ZIP_ERRNO; 1581 | } 1582 | 1583 | if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw)) 1584 | { 1585 | int tmp_err = deflateEnd(&zi->ci.stream); 1586 | if (err == ZIP_OK) 1587 | err = tmp_err; 1588 | zi->ci.stream_initialised = 0; 1589 | } 1590 | #ifdef HAVE_BZIP2 1591 | else if((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw)) 1592 | { 1593 | int tmperr = BZ2_bzCompressEnd(&zi->ci.bstream); 1594 | if (err==ZIP_OK) 1595 | err = tmperr; 1596 | zi->ci.stream_initialised = 0; 1597 | } 1598 | #endif 1599 | 1600 | if (!zi->ci.raw) 1601 | { 1602 | crc32 = (uLong)zi->ci.crc32; 1603 | uncompressed_size = zi->ci.totalUncompressedData; 1604 | } 1605 | compressed_size = zi->ci.totalCompressedData; 1606 | 1607 | # ifndef NOCRYPT 1608 | compressed_size += zi->ci.crypt_header_size; 1609 | # endif 1610 | 1611 | // update Current Item crc and sizes, 1612 | if(compressed_size >= 0xffffffff || uncompressed_size >= 0xffffffff || zi->ci.pos_local_header >= 0xffffffff) 1613 | { 1614 | /*version Made by*/ 1615 | zip64local_putValue_inmemory(zi->ci.central_header+4,(uLong)45,2); 1616 | /*version needed*/ 1617 | zip64local_putValue_inmemory(zi->ci.central_header+6,(uLong)45,2); 1618 | 1619 | } 1620 | 1621 | zip64local_putValue_inmemory(zi->ci.central_header+16,crc32,4); /*crc*/ 1622 | 1623 | 1624 | if(compressed_size >= 0xffffffff) 1625 | zip64local_putValue_inmemory(zi->ci.central_header+20, invalidValue,4); /*compr size*/ 1626 | else 1627 | zip64local_putValue_inmemory(zi->ci.central_header+20, compressed_size,4); /*compr size*/ 1628 | 1629 | /// set internal file attributes field 1630 | if (zi->ci.stream.data_type == Z_ASCII) 1631 | zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)Z_ASCII,2); 1632 | 1633 | if(uncompressed_size >= 0xffffffff) 1634 | zip64local_putValue_inmemory(zi->ci.central_header+24, invalidValue,4); /*uncompr size*/ 1635 | else 1636 | zip64local_putValue_inmemory(zi->ci.central_header+24, uncompressed_size,4); /*uncompr size*/ 1637 | 1638 | // Add ZIP64 extra info field for uncompressed size 1639 | if(uncompressed_size >= 0xffffffff) 1640 | datasize += 8; 1641 | 1642 | // Add ZIP64 extra info field for compressed size 1643 | if(compressed_size >= 0xffffffff) 1644 | datasize += 8; 1645 | 1646 | // Add ZIP64 extra info field for relative offset to local file header of current file 1647 | if(zi->ci.pos_local_header >= 0xffffffff) 1648 | datasize += 8; 1649 | 1650 | if(datasize > 0) 1651 | { 1652 | char* p = NULL; 1653 | 1654 | if((uLong)(datasize + 4) > zi->ci.size_centralExtraFree) 1655 | { 1656 | // we can not write more data to the buffer that we have room for. 1657 | return ZIP_BADZIPFILE; 1658 | } 1659 | 1660 | p = zi->ci.central_header + zi->ci.size_centralheader; 1661 | 1662 | // Add Extra Information Header for 'ZIP64 information' 1663 | zip64local_putValue_inmemory(p, 0x0001, 2); // HeaderID 1664 | p += 2; 1665 | zip64local_putValue_inmemory(p, datasize, 2); // DataSize 1666 | p += 2; 1667 | 1668 | if(uncompressed_size >= 0xffffffff) 1669 | { 1670 | zip64local_putValue_inmemory(p, uncompressed_size, 8); 1671 | p += 8; 1672 | } 1673 | 1674 | if(compressed_size >= 0xffffffff) 1675 | { 1676 | zip64local_putValue_inmemory(p, compressed_size, 8); 1677 | p += 8; 1678 | } 1679 | 1680 | if(zi->ci.pos_local_header >= 0xffffffff) 1681 | { 1682 | zip64local_putValue_inmemory(p, zi->ci.pos_local_header, 8); 1683 | p += 8; 1684 | } 1685 | 1686 | // Update how much extra free space we got in the memory buffer 1687 | // and increase the centralheader size so the new ZIP64 fields are included 1688 | // ( 4 below is the size of HeaderID and DataSize field ) 1689 | zi->ci.size_centralExtraFree -= datasize + 4; 1690 | zi->ci.size_centralheader += datasize + 4; 1691 | 1692 | // Update the extra info size field 1693 | zi->ci.size_centralExtra += datasize + 4; 1694 | zip64local_putValue_inmemory(zi->ci.central_header+30,(uLong)zi->ci.size_centralExtra,2); 1695 | } 1696 | 1697 | if (err==ZIP_OK) 1698 | err = add_data_in_datablock(&zi->central_dir, zi->ci.central_header, (uLong)zi->ci.size_centralheader); 1699 | 1700 | free(zi->ci.central_header); 1701 | 1702 | if (err==ZIP_OK) 1703 | { 1704 | // Update the LocalFileHeader with the new values. 1705 | 1706 | ZPOS64_T cur_pos_inzip = ZTELL64(zi->z_filefunc,zi->filestream); 1707 | 1708 | if (ZSEEK64(zi->z_filefunc,zi->filestream, zi->ci.pos_local_header + 14,ZLIB_FILEFUNC_SEEK_SET)!=0) 1709 | err = ZIP_ERRNO; 1710 | 1711 | if (err==ZIP_OK) 1712 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,crc32,4); /* crc 32, unknown */ 1713 | 1714 | if(uncompressed_size >= 0xffffffff || compressed_size >= 0xffffffff ) 1715 | { 1716 | if(zi->ci.pos_zip64extrainfo > 0) 1717 | { 1718 | // Update the size in the ZIP64 extended field. 1719 | if (ZSEEK64(zi->z_filefunc,zi->filestream, zi->ci.pos_zip64extrainfo + 4,ZLIB_FILEFUNC_SEEK_SET)!=0) 1720 | err = ZIP_ERRNO; 1721 | 1722 | if (err==ZIP_OK) /* compressed size, unknown */ 1723 | err = zip64local_putValue(&zi->z_filefunc, zi->filestream, uncompressed_size, 8); 1724 | 1725 | if (err==ZIP_OK) /* uncompressed size, unknown */ 1726 | err = zip64local_putValue(&zi->z_filefunc, zi->filestream, compressed_size, 8); 1727 | } 1728 | else 1729 | err = ZIP_BADZIPFILE; // Caller passed zip64 = 0, so no room for zip64 info -> fatal 1730 | } 1731 | else 1732 | { 1733 | if (err==ZIP_OK) /* compressed size, unknown */ 1734 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,compressed_size,4); 1735 | 1736 | if (err==ZIP_OK) /* uncompressed size, unknown */ 1737 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,uncompressed_size,4); 1738 | } 1739 | 1740 | if (ZSEEK64(zi->z_filefunc,zi->filestream, cur_pos_inzip,ZLIB_FILEFUNC_SEEK_SET)!=0) 1741 | err = ZIP_ERRNO; 1742 | } 1743 | 1744 | zi->number_entry ++; 1745 | zi->in_opened_file_inzip = 0; 1746 | 1747 | return err; 1748 | } 1749 | 1750 | extern int ZEXPORT zipCloseFileInZip (zipFile file) 1751 | { 1752 | return zipCloseFileInZipRaw (file,0,0); 1753 | } 1754 | 1755 | int Write_Zip64EndOfCentralDirectoryLocator(zip64_internal* zi, ZPOS64_T zip64eocd_pos_inzip) 1756 | { 1757 | int err = ZIP_OK; 1758 | ZPOS64_T pos = zip64eocd_pos_inzip - zi->add_position_when_writting_offset; 1759 | 1760 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ZIP64ENDLOCHEADERMAGIC,4); 1761 | 1762 | /*num disks*/ 1763 | if (err==ZIP_OK) /* number of the disk with the start of the central directory */ 1764 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); 1765 | 1766 | /*relative offset*/ 1767 | if (err==ZIP_OK) /* Relative offset to the Zip64EndOfCentralDirectory */ 1768 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream, pos,8); 1769 | 1770 | /*total disks*/ /* Do not support spawning of disk so always say 1 here*/ 1771 | if (err==ZIP_OK) /* number of the disk with the start of the central directory */ 1772 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)1,4); 1773 | 1774 | return err; 1775 | } 1776 | 1777 | int Write_Zip64EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip) 1778 | { 1779 | int err = ZIP_OK; 1780 | 1781 | uLong Zip64DataSize = 44; 1782 | 1783 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ZIP64ENDHEADERMAGIC,4); 1784 | 1785 | if (err==ZIP_OK) /* size of this 'zip64 end of central directory' */ 1786 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(ZPOS64_T)Zip64DataSize,8); // why ZPOS64_T of this ? 1787 | 1788 | if (err==ZIP_OK) /* version made by */ 1789 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2); 1790 | 1791 | if (err==ZIP_OK) /* version needed */ 1792 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2); 1793 | 1794 | if (err==ZIP_OK) /* number of this disk */ 1795 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); 1796 | 1797 | if (err==ZIP_OK) /* number of the disk with the start of the central directory */ 1798 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); 1799 | 1800 | if (err==ZIP_OK) /* total number of entries in the central dir on this disk */ 1801 | err = zip64local_putValue(&zi->z_filefunc, zi->filestream, zi->number_entry, 8); 1802 | 1803 | if (err==ZIP_OK) /* total number of entries in the central dir */ 1804 | err = zip64local_putValue(&zi->z_filefunc, zi->filestream, zi->number_entry, 8); 1805 | 1806 | if (err==ZIP_OK) /* size of the central directory */ 1807 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(ZPOS64_T)size_centraldir,8); 1808 | 1809 | if (err==ZIP_OK) /* offset of start of central directory with respect to the starting disk number */ 1810 | { 1811 | ZPOS64_T pos = centraldir_pos_inzip - zi->add_position_when_writting_offset; 1812 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (ZPOS64_T)pos,8); 1813 | } 1814 | return err; 1815 | } 1816 | int Write_EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip) 1817 | { 1818 | int err = ZIP_OK; 1819 | 1820 | /*signature*/ 1821 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ENDHEADERMAGIC,4); 1822 | 1823 | if (err==ZIP_OK) /* number of this disk */ 1824 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2); 1825 | 1826 | if (err==ZIP_OK) /* number of the disk with the start of the central directory */ 1827 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2); 1828 | 1829 | if (err==ZIP_OK) /* total number of entries in the central dir on this disk */ 1830 | { 1831 | { 1832 | if(zi->number_entry >= 0xFFFF) 1833 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xffff,2); // use value in ZIP64 record 1834 | else 1835 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2); 1836 | } 1837 | } 1838 | 1839 | if (err==ZIP_OK) /* total number of entries in the central dir */ 1840 | { 1841 | if(zi->number_entry >= 0xFFFF) 1842 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xffff,2); // use value in ZIP64 record 1843 | else 1844 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2); 1845 | } 1846 | 1847 | if (err==ZIP_OK) /* size of the central directory */ 1848 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_centraldir,4); 1849 | 1850 | if (err==ZIP_OK) /* offset of start of central directory with respect to the starting disk number */ 1851 | { 1852 | ZPOS64_T pos = centraldir_pos_inzip - zi->add_position_when_writting_offset; 1853 | if(pos >= 0xffffffff) 1854 | { 1855 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (uLong)0xffffffff,4); 1856 | } 1857 | else 1858 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (uLong)(centraldir_pos_inzip - zi->add_position_when_writting_offset),4); 1859 | } 1860 | 1861 | return err; 1862 | } 1863 | 1864 | int Write_GlobalComment(zip64_internal* zi, const char* global_comment) 1865 | { 1866 | int err = ZIP_OK; 1867 | uInt size_global_comment = 0; 1868 | 1869 | if(global_comment != NULL) 1870 | size_global_comment = (uInt)strlen(global_comment); 1871 | 1872 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_global_comment,2); 1873 | 1874 | if (err == ZIP_OK && size_global_comment > 0) 1875 | { 1876 | if (ZWRITE64(zi->z_filefunc,zi->filestream, global_comment, size_global_comment) != size_global_comment) 1877 | err = ZIP_ERRNO; 1878 | } 1879 | return err; 1880 | } 1881 | 1882 | extern int ZEXPORT zipClose (zipFile file, const char* global_comment) 1883 | { 1884 | zip64_internal* zi; 1885 | int err = 0; 1886 | uLong size_centraldir = 0; 1887 | ZPOS64_T centraldir_pos_inzip; 1888 | ZPOS64_T pos; 1889 | 1890 | if (file == NULL) 1891 | return ZIP_PARAMERROR; 1892 | 1893 | zi = (zip64_internal*)file; 1894 | 1895 | if (zi->in_opened_file_inzip == 1) 1896 | { 1897 | err = zipCloseFileInZip (file); 1898 | } 1899 | 1900 | #ifndef NO_ADDFILEINEXISTINGZIP 1901 | if (global_comment==NULL) 1902 | global_comment = zi->globalcomment; 1903 | #endif 1904 | 1905 | centraldir_pos_inzip = ZTELL64(zi->z_filefunc,zi->filestream); 1906 | 1907 | if (err==ZIP_OK) 1908 | { 1909 | linkedlist_datablock_internal* ldi = zi->central_dir.first_block; 1910 | while (ldi!=NULL) 1911 | { 1912 | if ((err==ZIP_OK) && (ldi->filled_in_this_block>0)) 1913 | { 1914 | if (ZWRITE64(zi->z_filefunc,zi->filestream, ldi->data, ldi->filled_in_this_block) != ldi->filled_in_this_block) 1915 | err = ZIP_ERRNO; 1916 | } 1917 | 1918 | size_centraldir += ldi->filled_in_this_block; 1919 | ldi = ldi->next_datablock; 1920 | } 1921 | } 1922 | free_linkedlist(&(zi->central_dir)); 1923 | 1924 | pos = centraldir_pos_inzip - zi->add_position_when_writting_offset; 1925 | if(pos >= 0xffffffff || zi->number_entry > 0xFFFF) 1926 | { 1927 | ZPOS64_T Zip64EOCDpos = ZTELL64(zi->z_filefunc,zi->filestream); 1928 | Write_Zip64EndOfCentralDirectoryRecord(zi, size_centraldir, centraldir_pos_inzip); 1929 | 1930 | Write_Zip64EndOfCentralDirectoryLocator(zi, Zip64EOCDpos); 1931 | } 1932 | 1933 | if (err==ZIP_OK) 1934 | err = Write_EndOfCentralDirectoryRecord(zi, size_centraldir, centraldir_pos_inzip); 1935 | 1936 | if(err == ZIP_OK) 1937 | err = Write_GlobalComment(zi, global_comment); 1938 | 1939 | if (ZCLOSE64(zi->z_filefunc,zi->filestream) != 0) 1940 | if (err == ZIP_OK) 1941 | err = ZIP_ERRNO; 1942 | 1943 | #ifndef NO_ADDFILEINEXISTINGZIP 1944 | TRYFREE(zi->globalcomment); 1945 | #endif 1946 | TRYFREE(zi); 1947 | 1948 | return err; 1949 | } 1950 | 1951 | extern int ZEXPORT zipRemoveExtraInfoBlock (char* pData, int* dataLen, short sHeader) 1952 | { 1953 | char* p = pData; 1954 | int size = 0; 1955 | char* pNewHeader; 1956 | char* pTmp; 1957 | short header; 1958 | short dataSize; 1959 | 1960 | int retVal = ZIP_OK; 1961 | 1962 | if(pData == NULL || *dataLen < 4) 1963 | return ZIP_PARAMERROR; 1964 | 1965 | pNewHeader = (char*)ALLOC(*dataLen); 1966 | pTmp = pNewHeader; 1967 | 1968 | while(p < (pData + *dataLen)) 1969 | { 1970 | header = *(short*)p; 1971 | dataSize = *(((short*)p)+1); 1972 | 1973 | if( header == sHeader ) // Header found. 1974 | { 1975 | p += dataSize + 4; // skip it. do not copy to temp buffer 1976 | } 1977 | else 1978 | { 1979 | // Extra Info block should not be removed, So copy it to the temp buffer. 1980 | memcpy(pTmp, p, dataSize + 4); 1981 | p += dataSize + 4; 1982 | size += dataSize + 4; 1983 | } 1984 | 1985 | } 1986 | 1987 | if(size < *dataLen) 1988 | { 1989 | // clean old extra info block. 1990 | memset(pData,0, *dataLen); 1991 | 1992 | // copy the new extra info block over the old 1993 | if(size > 0) 1994 | memcpy(pData, pNewHeader, size); 1995 | 1996 | // set the new extra info size 1997 | *dataLen = size; 1998 | 1999 | retVal = ZIP_OK; 2000 | } 2001 | else 2002 | retVal = ZIP_ERRNO; 2003 | 2004 | TRYFREE(pNewHeader); 2005 | 2006 | return retVal; 2007 | } 2008 | -------------------------------------------------------------------------------- /Source/ZipPlatformFile/Private/minizip/zip.h: -------------------------------------------------------------------------------- 1 | /* zip.h -- IO on .zip files using zlib 2 | Version 1.1, February 14h, 2010 3 | part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html ) 4 | 5 | Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html ) 6 | 7 | Modifications for Zip64 support 8 | Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com ) 9 | 10 | For more info read MiniZip_info.txt 11 | 12 | --------------------------------------------------------------------------- 13 | 14 | Condition of use and distribution are the same than zlib : 15 | 16 | This software is provided 'as-is', without any express or implied 17 | warranty. In no event will the authors be held liable for any damages 18 | arising from the use of this software. 19 | 20 | Permission is granted to anyone to use this software for any purpose, 21 | including commercial applications, and to alter it and redistribute it 22 | freely, subject to the following restrictions: 23 | 24 | 1. The origin of this software must not be misrepresented; you must not 25 | claim that you wrote the original software. If you use this software 26 | in a product, an acknowledgment in the product documentation would be 27 | appreciated but is not required. 28 | 2. Altered source versions must be plainly marked as such, and must not be 29 | misrepresented as being the original software. 30 | 3. This notice may not be removed or altered from any source distribution. 31 | 32 | --------------------------------------------------------------------------- 33 | 34 | Changes 35 | 36 | See header of zip.h 37 | 38 | */ 39 | 40 | #ifndef _zip12_H 41 | #define _zip12_H 42 | 43 | #ifdef __cplusplus 44 | extern "C" { 45 | #endif 46 | 47 | //#define HAVE_BZIP2 48 | 49 | #ifndef _ZLIB_H 50 | #include "zlib.h" 51 | #endif 52 | 53 | #ifndef _ZLIBIOAPI_H 54 | #include "ioapi.h" 55 | #endif 56 | 57 | #ifdef HAVE_BZIP2 58 | #include "bzlib.h" 59 | #endif 60 | 61 | #define Z_BZIP2ED 12 62 | 63 | #if defined(STRICTZIP) || defined(STRICTZIPUNZIP) 64 | /* like the STRICT of WIN32, we define a pointer that cannot be converted 65 | from (void*) without cast */ 66 | typedef struct TagzipFile__ { int unused; } zipFile__; 67 | typedef zipFile__ *zipFile; 68 | #else 69 | typedef voidp zipFile; 70 | #endif 71 | 72 | #define ZIP_OK (0) 73 | #define ZIP_EOF (0) 74 | #define ZIP_ERRNO (Z_ERRNO) 75 | #define ZIP_PARAMERROR (-102) 76 | #define ZIP_BADZIPFILE (-103) 77 | #define ZIP_INTERNALERROR (-104) 78 | 79 | #ifndef DEF_MEM_LEVEL 80 | # if MAX_MEM_LEVEL >= 8 81 | # define DEF_MEM_LEVEL 8 82 | # else 83 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL 84 | # endif 85 | #endif 86 | /* default memLevel */ 87 | 88 | /* tm_zip contain date/time info */ 89 | typedef struct tm_zip_s 90 | { 91 | uInt tm_sec; /* seconds after the minute - [0,59] */ 92 | uInt tm_min; /* minutes after the hour - [0,59] */ 93 | uInt tm_hour; /* hours since midnight - [0,23] */ 94 | uInt tm_mday; /* day of the month - [1,31] */ 95 | uInt tm_mon; /* months since January - [0,11] */ 96 | uInt tm_year; /* years - [1980..2044] */ 97 | } tm_zip; 98 | 99 | typedef struct 100 | { 101 | tm_zip tmz_date; /* date in understandable format */ 102 | uLong dosDate; /* if dos_date == 0, tmu_date is used */ 103 | /* uLong flag; */ /* general purpose bit flag 2 bytes */ 104 | 105 | uLong internal_fa; /* internal file attributes 2 bytes */ 106 | uLong external_fa; /* external file attributes 4 bytes */ 107 | } zip_fileinfo; 108 | 109 | typedef const char* zipcharpc; 110 | 111 | 112 | #define APPEND_STATUS_CREATE (0) 113 | #define APPEND_STATUS_CREATEAFTER (1) 114 | #define APPEND_STATUS_ADDINZIP (2) 115 | 116 | extern zipFile ZEXPORT zipOpen OF((const char *pathname, int append)); 117 | extern zipFile ZEXPORT zipOpen64 OF((const void *pathname, int append)); 118 | /* 119 | Create a zipfile. 120 | pathname contain on Windows XP a filename like "c:\\zlib\\zlib113.zip" or on 121 | an Unix computer "zlib/zlib113.zip". 122 | if the file pathname exist and append==APPEND_STATUS_CREATEAFTER, the zip 123 | will be created at the end of the file. 124 | (useful if the file contain a self extractor code) 125 | if the file pathname exist and append==APPEND_STATUS_ADDINZIP, we will 126 | add files in existing zip (be sure you don't add file that doesn't exist) 127 | If the zipfile cannot be opened, the return value is NULL. 128 | Else, the return value is a zipFile Handle, usable with other function 129 | of this zip package. 130 | */ 131 | 132 | /* Note : there is no delete function into a zipfile. 133 | If you want delete file into a zipfile, you must open a zipfile, and create another 134 | Of couse, you can use RAW reading and writing to copy the file you did not want delte 135 | */ 136 | 137 | extern zipFile ZEXPORT zipOpen2 OF((const char *pathname, 138 | int append, 139 | zipcharpc* globalcomment, 140 | zlib_filefunc_def* pzlib_filefunc_def)); 141 | 142 | extern zipFile ZEXPORT zipOpen2_64 OF((const void *pathname, 143 | int append, 144 | zipcharpc* globalcomment, 145 | zlib_filefunc64_def* pzlib_filefunc_def)); 146 | 147 | extern int ZEXPORT zipOpenNewFileInZip OF((zipFile file, 148 | const char* filename, 149 | const zip_fileinfo* zipfi, 150 | const void* extrafield_local, 151 | uInt size_extrafield_local, 152 | const void* extrafield_global, 153 | uInt size_extrafield_global, 154 | const char* comment, 155 | int method, 156 | int level)); 157 | 158 | extern int ZEXPORT zipOpenNewFileInZip64 OF((zipFile file, 159 | const char* filename, 160 | const zip_fileinfo* zipfi, 161 | const void* extrafield_local, 162 | uInt size_extrafield_local, 163 | const void* extrafield_global, 164 | uInt size_extrafield_global, 165 | const char* comment, 166 | int method, 167 | int level, 168 | int zip64)); 169 | 170 | /* 171 | Open a file in the ZIP for writing. 172 | filename : the filename in zip (if NULL, '-' without quote will be used 173 | *zipfi contain supplemental information 174 | if extrafield_local!=NULL and size_extrafield_local>0, extrafield_local 175 | contains the extrafield data the the local header 176 | if extrafield_global!=NULL and size_extrafield_global>0, extrafield_global 177 | contains the extrafield data the the local header 178 | if comment != NULL, comment contain the comment string 179 | method contain the compression method (0 for store, Z_DEFLATED for deflate) 180 | level contain the level of compression (can be Z_DEFAULT_COMPRESSION) 181 | zip64 is set to 1 if a zip64 extended information block should be added to the local file header. 182 | this MUST be '1' if the uncompressed size is >= 0xffffffff. 183 | 184 | */ 185 | 186 | 187 | extern int ZEXPORT zipOpenNewFileInZip2 OF((zipFile file, 188 | const char* filename, 189 | const zip_fileinfo* zipfi, 190 | const void* extrafield_local, 191 | uInt size_extrafield_local, 192 | const void* extrafield_global, 193 | uInt size_extrafield_global, 194 | const char* comment, 195 | int method, 196 | int level, 197 | int raw)); 198 | 199 | 200 | extern int ZEXPORT zipOpenNewFileInZip2_64 OF((zipFile file, 201 | const char* filename, 202 | const zip_fileinfo* zipfi, 203 | const void* extrafield_local, 204 | uInt size_extrafield_local, 205 | const void* extrafield_global, 206 | uInt size_extrafield_global, 207 | const char* comment, 208 | int method, 209 | int level, 210 | int raw, 211 | int zip64)); 212 | /* 213 | Same than zipOpenNewFileInZip, except if raw=1, we write raw file 214 | */ 215 | 216 | extern int ZEXPORT zipOpenNewFileInZip3 OF((zipFile file, 217 | const char* filename, 218 | const zip_fileinfo* zipfi, 219 | const void* extrafield_local, 220 | uInt size_extrafield_local, 221 | const void* extrafield_global, 222 | uInt size_extrafield_global, 223 | const char* comment, 224 | int method, 225 | int level, 226 | int raw, 227 | int windowBits, 228 | int memLevel, 229 | int strategy, 230 | const char* password, 231 | uLong crcForCrypting)); 232 | 233 | extern int ZEXPORT zipOpenNewFileInZip3_64 OF((zipFile file, 234 | const char* filename, 235 | const zip_fileinfo* zipfi, 236 | const void* extrafield_local, 237 | uInt size_extrafield_local, 238 | const void* extrafield_global, 239 | uInt size_extrafield_global, 240 | const char* comment, 241 | int method, 242 | int level, 243 | int raw, 244 | int windowBits, 245 | int memLevel, 246 | int strategy, 247 | const char* password, 248 | uLong crcForCrypting, 249 | int zip64 250 | )); 251 | 252 | /* 253 | Same than zipOpenNewFileInZip2, except 254 | windowBits,memLevel,,strategy : see parameter strategy in deflateInit2 255 | password : crypting password (NULL for no crypting) 256 | crcForCrypting : crc of file to compress (needed for crypting) 257 | */ 258 | 259 | extern int ZEXPORT zipOpenNewFileInZip4 OF((zipFile file, 260 | const char* filename, 261 | const zip_fileinfo* zipfi, 262 | const void* extrafield_local, 263 | uInt size_extrafield_local, 264 | const void* extrafield_global, 265 | uInt size_extrafield_global, 266 | const char* comment, 267 | int method, 268 | int level, 269 | int raw, 270 | int windowBits, 271 | int memLevel, 272 | int strategy, 273 | const char* password, 274 | uLong crcForCrypting, 275 | uLong versionMadeBy, 276 | uLong flagBase 277 | )); 278 | 279 | 280 | extern int ZEXPORT zipOpenNewFileInZip4_64 OF((zipFile file, 281 | const char* filename, 282 | const zip_fileinfo* zipfi, 283 | const void* extrafield_local, 284 | uInt size_extrafield_local, 285 | const void* extrafield_global, 286 | uInt size_extrafield_global, 287 | const char* comment, 288 | int method, 289 | int level, 290 | int raw, 291 | int windowBits, 292 | int memLevel, 293 | int strategy, 294 | const char* password, 295 | uLong crcForCrypting, 296 | uLong versionMadeBy, 297 | uLong flagBase, 298 | int zip64 299 | )); 300 | /* 301 | Same than zipOpenNewFileInZip4, except 302 | versionMadeBy : value for Version made by field 303 | flag : value for flag field (compression level info will be added) 304 | */ 305 | 306 | 307 | extern int ZEXPORT zipWriteInFileInZip OF((zipFile file, 308 | const void* buf, 309 | unsigned len)); 310 | /* 311 | Write data in the zipfile 312 | */ 313 | 314 | extern int ZEXPORT zipCloseFileInZip OF((zipFile file)); 315 | /* 316 | Close the current file in the zipfile 317 | */ 318 | 319 | extern int ZEXPORT zipCloseFileInZipRaw OF((zipFile file, 320 | uLong uncompressed_size, 321 | uLong crc32)); 322 | 323 | extern int ZEXPORT zipCloseFileInZipRaw64 OF((zipFile file, 324 | ZPOS64_T uncompressed_size, 325 | uLong crc32)); 326 | 327 | /* 328 | Close the current file in the zipfile, for file opened with 329 | parameter raw=1 in zipOpenNewFileInZip2 330 | uncompressed_size and crc32 are value for the uncompressed size 331 | */ 332 | 333 | extern int ZEXPORT zipClose OF((zipFile file, 334 | const char* global_comment)); 335 | /* 336 | Close the zipfile 337 | */ 338 | 339 | 340 | extern int ZEXPORT zipRemoveExtraInfoBlock OF((char* pData, int* dataLen, short sHeader)); 341 | /* 342 | zipRemoveExtraInfoBlock - Added by Mathias Svensson 343 | 344 | Remove extra information block from a extra information data for the local file header or central directory header 345 | 346 | It is needed to remove ZIP64 extra information blocks when before data is written if using RAW mode. 347 | 348 | 0x0001 is the signature header for the ZIP64 extra information blocks 349 | 350 | usage. 351 | Remove ZIP64 Extra information from a central director extra field data 352 | zipRemoveExtraInfoBlock(pCenDirExtraFieldData, &nCenDirExtraFieldDataLen, 0x0001); 353 | 354 | Remove ZIP64 Extra information from a Local File Header extra field data 355 | zipRemoveExtraInfoBlock(pLocalHeaderExtraFieldData, &nLocalHeaderExtraFieldDataLen, 0x0001); 356 | */ 357 | 358 | #ifdef __cplusplus 359 | } 360 | #endif 361 | 362 | #endif /* _zip64_H */ 363 | -------------------------------------------------------------------------------- /Source/ZipPlatformFile/Public/IZipPlatformFile.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | class ZIPPLATFORMFILE_API IZipPlatformFile : public IPlatformFile 6 | { 7 | public: 8 | virtual bool IsMounted(const TCHAR* Filename) const = 0; 9 | virtual bool Mount(const TCHAR* MountPoint, const TCHAR* Filename, const TCHAR* Password) = 0; 10 | virtual bool Unmount(const TCHAR* Filename) = 0; 11 | }; 12 | -------------------------------------------------------------------------------- /Source/ZipPlatformFile/ZipPlatformFile.Build.cs: -------------------------------------------------------------------------------- 1 | using UnrealBuildTool; 2 | 3 | public class ZipPlatformFile : ModuleRules 4 | { 5 | public ZipPlatformFile(ReadOnlyTargetRules Target) : base(Target) 6 | { 7 | PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; 8 | 9 | PublicDependencyModuleNames.AddRange(new string[] { 10 | "Core", 11 | "CoreUObject", 12 | "Engine", 13 | }); 14 | 15 | PrivateDependencyModuleNames.AddRange(new string[] { 16 | "zlib" 17 | }); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Source/ZipPlatformFileEd/Private/ZipPlatformFileEdModule.cpp: -------------------------------------------------------------------------------- 1 | #include "ZipPlatformFileEdModule.h" 2 | 3 | #include "ZipPlatformFileSettings.h" 4 | 5 | #include 6 | #include 7 | 8 | DEFINE_LOG_CATEGORY(LogZipPlatformFileEd); 9 | 10 | #define LOCTEXT_NAMESPACE "FZipPlatformFileEdModule" 11 | 12 | class FZipPlatformFileEdModule : public IModuleInterface 13 | { 14 | public: 15 | FZipPlatformFileEdModule() 16 | { 17 | // 18 | } 19 | 20 | virtual ~FZipPlatformFileEdModule() 21 | { 22 | // 23 | } 24 | 25 | public: 26 | virtual void StartupModule() override 27 | { 28 | if (ISettingsModule* SettingsModule = FModuleManager::GetModulePtr("Settings")) 29 | { 30 | SettingsModule->RegisterSettings("Project", "Plugins", "ZipPlatformFile", 31 | LOCTEXT("SettingsName", "ZipPlatformFile"), 32 | LOCTEXT("SettingsDescription", "Settings of the ZipPlatformFile."), 33 | GetMutableDefault()); 34 | } 35 | } 36 | 37 | virtual void ShutdownModule() override 38 | { 39 | if (ISettingsModule* SettingsModule = FModuleManager::GetModulePtr("Settings")) 40 | { 41 | SettingsModule->UnregisterSettings("Project", "Plugins", "ZipPlatformFile"); 42 | } 43 | } 44 | }; 45 | 46 | #undef LOCTEXT_NAMESPACE 47 | 48 | IMPLEMENT_MODULE(FZipPlatformFileEdModule, ZipPlatformFileEd); 49 | -------------------------------------------------------------------------------- /Source/ZipPlatformFileEd/Private/ZipPlatformFileEdModule.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | DECLARE_LOG_CATEGORY_EXTERN(LogZipPlatformFileEd, Verbose, All); 6 | -------------------------------------------------------------------------------- /Source/ZipPlatformFileEd/ZipPlatformFileEd.Build.cs: -------------------------------------------------------------------------------- 1 | using UnrealBuildTool; 2 | 3 | public class ZipPlatformFileEd : ModuleRules 4 | { 5 | public ZipPlatformFileEd(ReadOnlyTargetRules Target) : base(Target) 6 | { 7 | PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; 8 | 9 | PublicDependencyModuleNames.AddRange(new string[] { 10 | "Core", 11 | "CoreUObject", 12 | "Engine", 13 | }); 14 | 15 | PrivateDependencyModuleNames.AddRange(new string[] { 16 | "Settings", 17 | "ZipPlatformFile", 18 | }); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ZipPlatformFile.uplugin: -------------------------------------------------------------------------------- 1 | { 2 | "FileVersion": 3, 3 | "Version": 1, 4 | "VersionName": "0.2", 5 | "FriendlyName": "ZipPlatformFile", 6 | "Description": "", 7 | "Category": "Other", 8 | "CreatedBy": "Xing Ji", 9 | "CreatedByURL": "https://jixing.ink", 10 | "DocsURL": "", 11 | "MarketplaceURL": "", 12 | "SupportURL": "", 13 | "CanContainContent": false, 14 | "IsBetaVersion": false, 15 | "IsExperimentalVersion": false, 16 | "Installed": true, 17 | "Modules": [ 18 | { 19 | "Name": "ZipPlatformFile", 20 | "Type": "Runtime", 21 | "LoadingPhase": "Default" 22 | }, 23 | { 24 | "Name": "ZipPlatformFileEd", 25 | "Type": "Editor", 26 | "LoadingPhase": "Default" 27 | } 28 | ] 29 | } --------------------------------------------------------------------------------