Issue #188: Optionally create a hosts.zip file in addition to the hosts file.
authorSteven Black <redacted>
Mon, 17 Oct 2016 03:07:06 +0000 (23:07 -0400)
committerSteven Black <redacted>
Mon, 17 Oct 2016 03:07:06 +0000 (23:07 -0400)
readme_template.md
updateHostsFile.py

index fa0c2d5046ca97e0c9d46116ae130f21bd7f305d..756515691815d38b7d6f7db57d62dd733508c04b 100644 (file)
@@ -76,6 +76,7 @@ exist, it will be created.
 
 `--skipstatichosts`, or `-s`: `false` (default) or `true`, omit the standard section, at the top containing lines like `127.0.0.1 localhost`.  This is useful for configuring proximate DNS services on the local network.
 
+`--zip`, or `-z`: `false` (default) or `true`, additionally create a zip archive of the hosts file named `hosts.zip`.
 
 ## How do I control which sources are unified?
 
index 82c73fa9631a2ecd674d7e72f7ea724f153345d0..f4c7e70f5d711684087f43fd701c8f38daecc1c8 100644 (file)
@@ -25,6 +25,8 @@ import glob
 import argparse
 import socket
 import json
+import zipfile
+import zlib
 
 # zip files are not used actually, support deleted
 # StringIO is not needed in Python 3
@@ -84,6 +86,7 @@ defaults = {
     "outputsubfolder" : "",
     "datafilenames" : "hosts",
     "targetip" : "0.0.0.0",
+    "ziphosts" : False,
     "updateurlfilename" : "update.info",
     "readmefilename" : "readme.md",
     "readmetemplate" : os.path.join(BASEDIR_PATH, "readme_template.md"),
@@ -102,6 +105,7 @@ def main():
     parser.add_argument("--backup", "-b", dest="backup", default=False, action="store_true", help="Backup the hosts files before they are overridden.")
     parser.add_argument("--extensions", "-e", dest="extensions", default=[], nargs="*", help="Host extensions to include in the final hosts file.")
     parser.add_argument("--ip", "-i", dest="targetip", default="0.0.0.0", help="Target IP address. Default is 0.0.0.0.")
+    parser.add_argument("--zip", "-z", dest="ziphosts", default=False, action="store_true", help="Additionally create a zip archive of the hosts file.")
     parser.add_argument("--noupdate", "-n", dest="noupdate", default=False, action="store_true", help="Don't update from host data sources.")
     parser.add_argument("--skipstatichosts", "-s", dest="skipstatichosts", default=False, action="store_true", help="Skip static localhost entries in the final hosts file.")
     parser.add_argument("--output", "-o", dest="outputsubfolder", default="", help="Output subfolder for generated hosts file.")
@@ -137,6 +141,12 @@ def main():
     removeOldHostsFile()
     finalFile = removeDupsAndExcl(mergeFile)
     finalizeFile(finalFile)
+
+    if settings["ziphosts"]:
+        zf = zipfile.ZipFile(os.path.join(settings["outputsubfolder"], "hosts.zip"), mode='w')
+        zf.write(os.path.join(settings["outputsubfolder"], "hosts"), compress_type=zipfile.ZIP_DEFLATED, arcname='hosts')
+        zf.close()
+
     updateReadmeData()
     printSuccess("Success! The hosts file has been saved in folder " + settings["outputsubfolder"] + "\nIt contains " +
                  "{:,}".format(settings["numberofrules"]) + " unique entries.")
git clone https://git.99rst.org/PROJECT