def zipdir(basedir, archivename): assert os.path.isdir(basedir) fos = FileOutputStream(archivename) zos = ZipOutputStream(fos) add_folder(zos, basedir, basedir) zos.close() return archivename
def fake_plugin_jar(name, plugins_config): output = FileOutputStream(name) zip = ZipOutputStream(output) entry = ZipEntry('plugins.config') zip.putNextEntry(entry) zip.write(plugins_config) zip.closeEntry() zip.close()
class ZipOutput(DirectoryOutput): def __init__(self, filename): self.zipfile = ZipOutputStream(FileOutputStream(filename)) def getName(self, name): return string.join(string.split(name, '.'), '/')+'.class' def getFile(self, name): fname = self.getName(name) self.zipfile.putNextEntry(ZipEntry(fname)) return self.zipfile def close(self): self.zipfile.close()
class ZipOutput(DirectoryOutput): def __init__(self, filename): self.zipfile = ZipOutputStream(FileOutputStream(filename)) def getName(self, name): return string.join(string.split(name, '.'), '/') + '.class' def getFile(self, name): fname = self.getName(name) self.zipfile.putNextEntry(ZipEntry(fname)) return self.zipfile def close(self): self.zipfile.close()
class Zip(object): def __init__(self, out): self.zipOutputStream = ZipOutputStream(out) def close(self): self.zipOutputStream.close() def addStream(self, name, s): self.zipOutputStream.putNextEntry(ZipEntry(name)) IOUtils.copy(s, self.zipOutputStream) self.zipOutputStream.closeEntry() def add(self, name, data): self.zipOutputStream.putNextEntry(ZipEntry(name)) self.__copyString(data, self.zipOutputStream) self.zipOutputStream.closeEntry() def __copyString(self, s, out): IOUtils.copy(IOUtils.toInputStream(String(s), "UTF-8"), out)
def __createPackage(self, outputFile=None): title = self.__manifest.getString(None, "title") manifest = self.__createManifest() context = JAXBContext.newInstance("com.googlecode.fascinator.ims") m = context.createMarshaller() m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, True) writer = StringWriter() jaxbElem = ObjectFactory.createManifest(ObjectFactory(), manifest) m.marshal(jaxbElem, writer) writer.close() if outputFile is not None: print "writing to %s..." % outputFile out = FileOutputStream(outputFile) else: print "writing to http output stream..." filename = urllib.quote(title.replace(" ", "_")) response.setHeader("Content-Disposition", "attachment; filename=%s.zip" % filename) out = response.getOutputStream("application/zip") zipOut = ZipOutputStream(out) zipOut.putNextEntry(ZipEntry("imsmanifest.xml")) IOUtils.write(writer.toString(), zipOut) zipOut.closeEntry() oidList = self.__manifest.search("id") for oid in oidList: obj = Services.getStorage().getObject(oid) for pid in obj.getPayloadIdList(): payload = obj.getPayload(pid) if not PayloadType.Annotation.equals(payload.getType()): zipOut.putNextEntry( ZipEntry("resources/%s/%s" % (oid, pid))) IOUtils.copy(payload.open(), zipOut) payload.close() zipOut.closeEntry() obj.close() zipOut.close() out.close()
def __createPackage(self, outputFile=None): title = self.__manifest.getString(None, "title") manifest = self.__createManifest() context = JAXBContext.newInstance("au.edu.usq.fascinator.ims") m = context.createMarshaller() m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, True) writer = StringWriter() jaxbElem = ObjectFactory.createManifest(ObjectFactory(), manifest) m.marshal(jaxbElem, writer) writer.close() if outputFile is not None: print "writing to %s..." % outputFile out = FileOutputStream(outputFile) else: print "writing to http output stream..." filename = urllib.quote(title.replace(" ", "_")) response.setHeader("Content-Disposition", "attachment; filename=%s.zip" % filename) out = response.getOutputStream("application/zip") zipOut = ZipOutputStream(out) zipOut.putNextEntry(ZipEntry("imsmanifest.xml")) IOUtils.write(writer.toString(), zipOut) zipOut.closeEntry() oidList = self.__manifest.search("id") for oid in oidList: obj = Services.getStorage().getObject(oid) for pid in obj.getPayloadIdList(): payload = obj.getPayload(pid) if not PayloadType.Annotation.equals(payload.getType()): zipOut.putNextEntry(ZipEntry("resources/%s/%s" % (oid, pid))) IOUtils.copy(payload.open(), zipOut) payload.close() zipOut.closeEntry() obj.close() zipOut.close() out.close()
def __createPackage(self, outputFile=None): manifest = self.__createManifest() context = JAXBContext.newInstance("au.edu.usq.fascinator.ims") m = context.createMarshaller() m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, True) writer = StringWriter() jaxbElem = ObjectFactory.createManifest(ObjectFactory(), manifest) m.marshal(jaxbElem, writer); writer.close() if outputFile is not None: print " * imscp.py: writing to %s..." % outputFile out = FileOutputStream(outputFile) else: print " * imscp.py: writing to http output stream..." response.setHeader("Content-Disposition", "attachment; filename=%s.zip" % self.__portal.getName()) out = response.getOutputStream("application/zip") zipOut = ZipOutputStream(out) zipOut.putNextEntry(ZipEntry("imsmanifest.xml")) IOUtils.write(writer.toString(), zipOut) zipOut.closeEntry() for key in self.__portalManifest.keySet(): item = self.__portalManifest.get(key) oid = item.get("id") obj = Services.getStorage().getObject(oid) for payload in obj.getPayloadList(): pid = payload.getId() if pid != "SOF-META": zipOut.putNextEntry(ZipEntry("%s/%s" % (key[5:], pid))) IOUtils.copy(payload.getInputStream(), zipOut) zipOut.closeEntry() zipOut.close() out.close()
verbose = False print 'Making', zipfile, 'from', folder if os.name == 'java': from java.io import FileOutputStream from java.util.zip import ZipOutputStream, ZipEntry def add_folder(zip, folder): for file in os.listdir(folder): file = folder + '/' + file if os.path.isdir(file): add_folder(zip, file) elif os.path.isfile(file): if verbose: print file entry = ZipEntry(file) zip.putNextEntry(entry) f = open(file, "rb") zip.write(f.read()) f.close() zip.closeEntry() output = FileOutputStream(zipfile) zip = ZipOutputStream(output) add_folder(zip, folder) zip.close() else: execute('zip -9r ' + zipfile + ' ' + folder)
def __createEpub(self): title = self.__manifest.get("title") response.setHeader("Content-Disposition", "attachment; filename=%s.epub" % urllib.quote(title)) out = response.getOutputStream("application/epub+zip") zipOutputStream = ZipOutputStream(out) #save mimetype... and the rest of standard files in epub zipOutputStream.putNextEntry(ZipEntry("mimetype")) epubMimetypeStream = self.__getResourceAsStream("/epub/mimetype") IOUtils.copy(epubMimetypeStream, zipOutputStream) zipOutputStream.closeEntry() zipOutputStream.putNextEntry(ZipEntry("META-INF/container.xml")) epubContainerStream = self.__getResourceAsStream("/epub/container.xml") IOUtils.copy(epubContainerStream, zipOutputStream) zipOutputStream.closeEntry() zipOutputStream.putNextEntry(ZipEntry("OEBPS/epub.css")) epubcss = self.__getResourceAsStream("/epub/epub.css") IOUtils.copy(epubcss, zipOutputStream) zipOutputStream.closeEntry() #### Creating toc.ncx #### tocXml = ElementTree.Element("ncx", {"version": "2005-1", "xml:lang":"en", "xmlns":"http://www.daisy.org/z3986/2005/ncx/"}) headNode = ElementTree.Element("head") tocXml.append(headNode) headNode.append(ElementTree.Element("meta", {"name": "dtb:uid", "content": "1"})) headNode.append(ElementTree.Element("meta", {"name": "dtb:depth", "content": "1"})) headNode.append(ElementTree.Element("meta", {"name": "dtb:totalPageCount", "content": "1"})) headNode.append(ElementTree.Element("meta", {"name": "dtb:maxPageNumber", "content": "1"})) headNode.append(ElementTree.Element("meta", {"name": "dtb:generator", "content": "ICE v2"})) #docTitle docTitle = ElementTree.Element("docTitle") textNode = ElementTree.Element("text") textNode.text = title docTitle.append(textNode) tocXml.append(docTitle) #docAuthor docAuthor = ElementTree.Element("docAuthor") textNode = ElementTree.Element("text") textNode.text = "ICE v2" docAuthor.append(textNode) tocXml.append(docAuthor) #navMap navMap = ElementTree.Element("navMap") tocXml.append(navMap) #### Creating content.opf #### contentXml = ElementTree.Element("package", {"version": "2.0", "xmlns":"http://www.idpf.org/2007/opf", "unique-identifier":"BookId"}) metadataNode = ElementTree.Element("metadata", {"xmlns:dc": "http://purl.org/dc/elements/1.1/", "xmlns:opf": "http://www.idpf.org/2007/opf"}) contentXml.append(metadataNode) #metadata information metadata = ElementTree.Element("dc:title") metadata.text = title metadataNode.append(metadata) metadata = ElementTree.Element("dc:language") metadata.text = "en-AU" metadataNode.append(metadata) metadata = ElementTree.Element("dc:creator", {"opf:role":"aut"}) metadata.text = "ICE" metadataNode.append(metadata) metadata = ElementTree.Element("dc:publisher") metadata.text = "University of Southern Queensland" metadataNode.append(metadata) metadata = ElementTree.Element("dc:identifier", {"id":"BookId"}) metadata.text = title metadataNode.append(metadata) #manifest manifest = ElementTree.Element("manifest") contentXml.append(manifest) spine = ElementTree.Element("spine", {"toc":"ncx"}) contentXml.append(spine) item = ElementTree.Element("item", {"id":"ncx", "href":"toc.ncx", "media-type":"text/xml"}) manifest.append(item) css = ElementTree.Element("item", {"id":"style", "href":"epub.css", "media-type":"text/css"}) manifest.append(css) count = 1 for itemHash in self.__orderedItem: id, title, htmlFileName, payloadDict, isImage = self.__itemRefDict[itemHash] for payloadId in payloadDict: payload, payloadType = payloadDict[payloadId] if isinstance(payload, Payload): payloadId = payloadId.lower() zipEntryId = payloadId.replace(" ", "_").replace("\\", "/") if payloadType == "application/xhtml+xml": zipOutputStream.putNextEntry(ZipEntry("OEBPS/%s" % zipEntryId)) ##process the html.... saxReader = SAXReader(False) try: saxDoc = saxReader.read(payload.open()) payload.close() # ## remove class or style nodes # classOrStyleNodes = saxDoc.selectNodes("//@class | //@style ") # for classOrStyleNode in classOrStyleNodes: # node = classOrStyleNode # if classOrStyleNode.getParent(): # node = classOrStyleNode.getParent() # if node.getQualifiedName() == "img": # attr = node.attribute(QName("class")) # attr = node.attribute(QName("class")) # if attr: # node.remove(attr) # attr = node.attribute(QName("style")) # if attr: # node.remove(attr) ## remove name in a tags ahrefs = saxDoc.selectNodes("//*[local-name()='a' and @name!='']") for a in ahrefs: attr = a.attribute(QName("name")) if attr: a.remove(attr) ## fix images src name.... replace space with underscore and all lower case imgs = saxDoc.selectNodes("//*[local-name()='img' and contains(@src, '_files')]") for img in imgs: srcAttr = img.attribute(QName("src")) if srcAttr: src = srcAttr.getValue() #hash the sourcename filepath, filename = os.path.split(src) filename, ext = os.path.splitext(filename) filename = hashlib.md5(filename).hexdigest() src = os.path.join(filepath.lower().replace(" ", "_"), "node-%s%s" % (filename, ext)) img.addAttribute(QName("src"), src.replace(" ", "_")) bodyNode = saxDoc.selectSingleNode("//*[local-name()='div' and @class='body']") bodyNode.setName("div") out = ByteArrayOutputStream() format = OutputFormat.createPrettyPrint() format.setSuppressDeclaration(True) writer = XMLWriter(out, format) writer.write(bodyNode) writer.flush() contentStr = out.toString("UTF-8") htmlString = """<?xml version="1.0" encoding="UTF-8"?> <html xmlns="http://www.w3.org/1999/xhtml"><head><title>%s</title> <link rel="stylesheet" href="epub.css"/> </head><body>%s</body></html>""" htmlString = htmlString % (title, contentStr) self.__copyString(htmlString, zipOutputStream) includeFile = False except: traceback.print_exc() else: #images.... zipOutputStream.putNextEntry(ZipEntry("OEBPS/%s" % zipEntryId)) IOUtils.copy(payload.open(), zipOutputStream) payload.close() zipOutputStream.closeEntry() else: zipOutputStream.putNextEntry(ZipEntry("OEBPS/%s" % zipEntryId)) IOUtils.copy(payload, zipOutputStream) zipOutputStream.closeEntry() itemNode = ElementTree.Element("item", {"media-type":payloadType, "href": zipEntryId}) if payloadId == htmlFileName.lower(): itemNode.set("id", itemHash) else: itemNode.set("id", payloadId.replace("/", "_")) manifest.append(itemNode) if not isImage: navPoint = ElementTree.Element("navPoint", {"class":"chapter", "id":"%s" % itemHash, "playOrder":"%s" % count}) else: navPoint = ElementTree.Element("navPoint", {"class":"chapter", "id":"%s" % htmlFileName, "playOrder":"%s" % count}) navMap.append(navPoint) navLabel = ElementTree.Element("navLabel") navPoint.append(navLabel) textNode = ElementTree.Element("text") textNode.text = title navLabel.append(textNode) content = ElementTree.Element("content") navPoint.append(content) content.set("src", htmlFileName) count +=1 itemRefNode = ElementTree.Element("itemref") spine.append(itemRefNode) itemRefNode.set("idref", itemHash) #saving content.opf... zipOutputStream.putNextEntry(ZipEntry("OEBPS/content.opf")) self.__copyString(ElementTree.tostring(contentXml), zipOutputStream) zipOutputStream.closeEntry() #saving toc.ncx zipOutputStream.putNextEntry(ZipEntry("OEBPS/toc.ncx")) self.__copyString(ElementTree.tostring(tocXml), zipOutputStream) zipOutputStream.closeEntry() zipOutputStream.close()
def __createEpub(self): title = self.__manifest.getString(None, "title") self.vc("response").setHeader( "Content-Disposition", "attachment; filename=%s.epub" % urllib.quote(title)) out = self.vc("response").getOutputStream("application/epub+zip") zipOutputStream = ZipOutputStream(out) #save mimetype... and the rest of standard files in epub zipOutputStream.putNextEntry(ZipEntry("mimetype")) epubMimetypeStream = self.__getResourceAsStream("/epub/mimetype") IOUtils.copy(epubMimetypeStream, zipOutputStream) zipOutputStream.closeEntry() zipOutputStream.putNextEntry(ZipEntry("META-INF/container.xml")) epubContainerStream = self.__getResourceAsStream("/epub/container.xml") IOUtils.copy(epubContainerStream, zipOutputStream) zipOutputStream.closeEntry() zipOutputStream.putNextEntry(ZipEntry("OEBPS/epub.css")) epubcss = self.__getResourceAsStream("/epub/epub.css") IOUtils.copy(epubcss, zipOutputStream) zipOutputStream.closeEntry() #### Creating toc.ncx #### tocXml = ElementTree.Element( "ncx", { "version": "2005-1", "xml:lang": "en", "xmlns": "http://www.daisy.org/z3986/2005/ncx/" }) headNode = ElementTree.Element("head") tocXml.append(headNode) headNode.append( ElementTree.Element("meta", { "name": "dtb:uid", "content": "1" })) headNode.append( ElementTree.Element("meta", { "name": "dtb:depth", "content": "1" })) headNode.append( ElementTree.Element("meta", { "name": "dtb:totalPageCount", "content": "1" })) headNode.append( ElementTree.Element("meta", { "name": "dtb:maxPageNumber", "content": "1" })) headNode.append( ElementTree.Element("meta", { "name": "dtb:generator", "content": "ICE v2" })) #docTitle docTitle = ElementTree.Element("docTitle") textNode = ElementTree.Element("text") textNode.text = title docTitle.append(textNode) tocXml.append(docTitle) #docAuthor docAuthor = ElementTree.Element("docAuthor") textNode = ElementTree.Element("text") textNode.text = "ICE v2" docAuthor.append(textNode) tocXml.append(docAuthor) #navMap navMap = ElementTree.Element("navMap") tocXml.append(navMap) #### Creating content.opf #### contentXml = ElementTree.Element( "package", { "version": "2.0", "xmlns": "http://www.idpf.org/2007/opf", "unique-identifier": "BookId" }) metadataNode = ElementTree.Element( "metadata", { "xmlns:dc": "http://purl.org/dc/elements/1.1/", "xmlns:opf": "http://www.idpf.org/2007/opf" }) contentXml.append(metadataNode) #metadata information metadata = ElementTree.Element("dc:title") metadata.text = title metadataNode.append(metadata) metadata = ElementTree.Element("dc:language") metadata.text = "en-AU" metadataNode.append(metadata) metadata = ElementTree.Element("dc:creator", {"opf:role": "aut"}) metadata.text = "ICE" metadataNode.append(metadata) metadata = ElementTree.Element("dc:publisher") metadata.text = "University of Southern Queensland" metadataNode.append(metadata) metadata = ElementTree.Element("dc:identifier", {"id": "BookId"}) metadata.text = title metadataNode.append(metadata) #manifest manifest = ElementTree.Element("manifest") contentXml.append(manifest) spine = ElementTree.Element("spine", {"toc": "ncx"}) contentXml.append(spine) item = ElementTree.Element("item", { "id": "ncx", "href": "toc.ncx", "media-type": "text/xml" }) manifest.append(item) css = ElementTree.Element("item", { "id": "style", "href": "epub.css", "media-type": "text/css" }) manifest.append(css) count = 1 for itemHash in self.__orderedItem: id, title, htmlFileName, payloadDict, isImage = self.__itemRefDict[ itemHash] for payloadId in payloadDict: payload, payloadType = payloadDict[payloadId] if isinstance(payload, Payload): payloadId = payloadId.lower() zipEntryId = payloadId.replace(" ", "_").replace("\\", "/") if payloadType == "application/xhtml+xml": zipOutputStream.putNextEntry( ZipEntry("OEBPS/%s" % zipEntryId)) ##process the html.... saxReader = SAXReader(False) try: saxDoc = saxReader.read(payload.open()) payload.close() # ## remove class or style nodes # classOrStyleNodes = saxDoc.selectNodes("//@class | //@style ") # for classOrStyleNode in classOrStyleNodes: # node = classOrStyleNode # if classOrStyleNode.getParent(): # node = classOrStyleNode.getParent() # if node.getQualifiedName() == "img": # attr = node.attribute(QName("class")) # attr = node.attribute(QName("class")) # if attr: # node.remove(attr) # attr = node.attribute(QName("style")) # if attr: # node.remove(attr) ## remove name in a tags ahrefs = saxDoc.selectNodes( "//*[local-name()='a' and @name!='']") for a in ahrefs: attr = a.attribute(QName("name")) if attr: a.remove(attr) ## fix images src name.... replace space with underscore and all lower case imgs = saxDoc.selectNodes( "//*[local-name()='img' and contains(@src, '_files')]" ) for img in imgs: srcAttr = img.attribute(QName("src")) if srcAttr: src = srcAttr.getValue() #hash the sourcename filepath, filename = os.path.split(src) filename, ext = os.path.splitext(filename) filename = hashlib.md5( filename).hexdigest() src = os.path.join( filepath.lower().replace(" ", "_"), "node-%s%s" % (filename, ext)) img.addAttribute(QName("src"), src.replace(" ", "_")) bodyNode = saxDoc.selectSingleNode( "//*[local-name()='div' and @class='body']") bodyNode.setName("div") out = ByteArrayOutputStream() format = OutputFormat.createPrettyPrint() format.setSuppressDeclaration(True) writer = XMLWriter(out, format) writer.write(bodyNode) writer.flush() contentStr = out.toString("UTF-8") htmlString = """<?xml version="1.0" encoding="UTF-8"?> <html xmlns="http://www.w3.org/1999/xhtml"><head><title>%s</title> <link rel="stylesheet" href="epub.css"/> </head><body>%s</body></html>""" htmlString = htmlString % (title, contentStr) self.__copyString(htmlString, zipOutputStream) includeFile = False except: traceback.print_exc() else: #images.... zipOutputStream.putNextEntry( ZipEntry("OEBPS/%s" % zipEntryId)) IOUtils.copy(payload.open(), zipOutputStream) payload.close() zipOutputStream.closeEntry() else: zipOutputStream.putNextEntry( ZipEntry("OEBPS/%s" % zipEntryId)) IOUtils.copy(payload, zipOutputStream) zipOutputStream.closeEntry() itemNode = ElementTree.Element("item", { "media-type": payloadType, "href": zipEntryId }) if payloadId == htmlFileName.lower(): itemNode.set("id", itemHash) else: itemNode.set("id", payloadId.replace("/", "_")) manifest.append(itemNode) if not isImage: navPoint = ElementTree.Element( "navPoint", { "class": "chapter", "id": "%s" % itemHash, "playOrder": "%s" % count }) else: navPoint = ElementTree.Element( "navPoint", { "class": "chapter", "id": "%s" % htmlFileName, "playOrder": "%s" % count }) navMap.append(navPoint) navLabel = ElementTree.Element("navLabel") navPoint.append(navLabel) textNode = ElementTree.Element("text") textNode.text = title navLabel.append(textNode) content = ElementTree.Element("content") navPoint.append(content) content.set("src", htmlFileName) count += 1 itemRefNode = ElementTree.Element("itemref") spine.append(itemRefNode) itemRefNode.set("idref", itemHash) #saving content.opf... zipOutputStream.putNextEntry(ZipEntry("OEBPS/content.opf")) self.__copyString(ElementTree.tostring(contentXml), zipOutputStream) zipOutputStream.closeEntry() #saving toc.ncx zipOutputStream.putNextEntry(ZipEntry("OEBPS/toc.ncx")) self.__copyString(ElementTree.tostring(tocXml), zipOutputStream) zipOutputStream.closeEntry() zipOutputStream.close()