aboutsummaryrefslogtreecommitdiffstats
path: root/gallery_dl/postprocessor/zip.py
diff options
context:
space:
mode:
authorLibravatarUnit 193 <unit193@ubuntu.com>2019-07-02 04:33:45 -0400
committerLibravatarUnit 193 <unit193@ubuntu.com>2019-07-02 04:33:45 -0400
commit195c45911e79c33cf0bb986721365fb06df5a153 (patch)
treeac0c9b6ef40bea7aa7ab0c5c3cb500eb510668fa /gallery_dl/postprocessor/zip.py
Import Upstream version 1.8.7upstream/1.8.7
Diffstat (limited to 'gallery_dl/postprocessor/zip.py')
-rw-r--r--gallery_dl/postprocessor/zip.py65
1 files changed, 65 insertions, 0 deletions
diff --git a/gallery_dl/postprocessor/zip.py b/gallery_dl/postprocessor/zip.py
new file mode 100644
index 0000000..3a0c323
--- /dev/null
+++ b/gallery_dl/postprocessor/zip.py
@@ -0,0 +1,65 @@
+# -*- coding: utf-8 -*-
+
+# Copyright 2018 Mike Fährmann
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 as
+# published by the Free Software Foundation.
+
+"""Store files in ZIP archives"""
+
+from .common import PostProcessor
+import zipfile
+import os
+
+
+class ZipPP(PostProcessor):
+
+ COMPRESSION_ALGORITHMS = {
+ "store": zipfile.ZIP_STORED,
+ "zip": zipfile.ZIP_DEFLATED,
+ "bzip2": zipfile.ZIP_BZIP2,
+ "lzma": zipfile.ZIP_LZMA,
+ }
+
+ def __init__(self, pathfmt, options):
+ PostProcessor.__init__(self)
+ self.delete = not options.get("keep-files", False)
+ self.ext = "." + options.get("extension", "zip")
+ algorithm = options.get("compression", "store")
+ if algorithm not in self.COMPRESSION_ALGORITHMS:
+ self.log.warning(
+ "unknown compression algorithm '%s'; falling back to 'store'",
+ algorithm)
+ algorithm = "store"
+
+ self.path = pathfmt.realdirectory
+ self.zfile = zipfile.ZipFile(
+ self.path + self.ext, "a",
+ self.COMPRESSION_ALGORITHMS[algorithm], True)
+
+ def run(self, pathfmt):
+ # 'NameToInfo' is not officially documented, but it's available
+ # for all supported Python versions and using it directly is a lot
+ # better than calling getinfo()
+ if pathfmt.filename not in self.zfile.NameToInfo:
+ self.zfile.write(pathfmt.temppath, pathfmt.filename)
+ pathfmt.delete = self.delete
+
+ def finalize(self):
+ self.zfile.close()
+
+ if self.delete:
+ try:
+ os.rmdir(self.path)
+ except OSError:
+ pass
+
+ if not self.zfile.NameToInfo:
+ try:
+ os.unlink(self.zfile.filename)
+ except OSError:
+ pass
+
+
+__postprocessor__ = ZipPP