def zippedResource(self, request, archiveName, name, *names): archivePath = self._elementsRoot.child("{0}.zip".format(archiveName)) try: filePath = ZipArchive(archivePath.path) except IOError: self.log.error("Zip archive not found: {archive.path}", archive=archivePath) return self.notFoundResource(request) except BadZipfile: self.log.error("Bad zip archive: {archive.path}", archive=archivePath) return self.notFoundResource(request) filePath = filePath.child(name) for name in names: filePath = filePath.child(name) try: return filePath.getContent() except KeyError: self.log.error( "File not found in ZIP archive: {filePath.path}", filePath=filePath, archive=archivePath, ) return self.notFoundResource(request)
async def cachedZippedResource( self, request: IRequest, url: URL, archiveName: str, name: str, *names: Any, ) -> KleinRenderable: """ Retrieve a cached resource from a zip file. """ archivePath = await self.cacheFromURL(url, f"{archiveName}.zip") try: filePath = ZipArchive(str(archivePath)) except BadZipfile as e: self._log.error( "Corrupt zip archive {path}: {error}", path=archivePath, error=e, ) try: archivePath.unlink() except (OSError, IOError) as e: self._log.critical( "Failed to remove corrupt zip archive {path}: {error}", path=archivePath, error=e, ) return internalErrorResponse(request) except (OSError, IOError) as e: self._log.critical( "Unable to open zip archive {path}: {error}", path=archivePath, error=e, ) return notFoundResponse(request) filePath = filePath.child(name) for name in names: filePath = filePath.child(name) try: return filePath.getContent() except KeyError: self._log.error( "File not found in ZIP archive: {filePath.path}", filePath=filePath, archive=archivePath, ) return notFoundResponse(request)
def cachedZippedResource(self, request, url, archiveName, name, *names): archivePath = yield self.cacheFromURL(url, "{0}.zip".format(archiveName)) try: filePath = ZipArchive(archivePath.path) except BadZipfile as e: self.log.error( "Corrupt zip archive {archive.path}: {error}", archive=archivePath, error=e, ) try: archivePath.remove() except (OSError, IOError): pass returnValue(self.notFoundResource(request)) except (OSError, IOError) as e: self.log.error( "Unable to open zip archive {archive.path}: {error}", archive=archivePath, error=e, ) returnValue(self.notFoundResource(request)) filePath = filePath.child(name) for name in names: filePath = filePath.child(name) try: returnValue(filePath.getContent()) except KeyError: self.log.error( "File not found in ZIP archive: {filePath.path}", filePath=filePath, archive=archivePath, ) returnValue(self.notFoundResource(request))
def readFromArchive(_): filePath = ZipArchive(archivePath.path) for segment in segments: filePath = filePath.child(segment) return filePath.getContent()