瀏覽代碼

Don't need to use cache for decompressed file

Stephen Dade 5 年之前
父節點
當前提交
9c54b6e9a6
共有 1 個文件被更改,包括 7 次插入11 次删除
  1. 7 11
      app.py

+ 7 - 11
app.py

@@ -5,6 +5,7 @@ import shutil
 import sys
 import urllib.request
 import gzip
+from io import BytesIO
 
 from flask import Flask
 from flask import render_template
@@ -36,10 +37,8 @@ def getDatFile(lat, lon):
 def compressFiles(fileList, uuidkey, outfolder):
     # create a zip file comprised of dat.gz tiles
     zipthis = os.path.join(this_path, outfolder, uuidkey + '.zip')
-    foldertmp = os.path.join(this_path, outfolder + "-tmp", uuidkey)
 
-    # create tmp and output dirs if needed
-    os.makedirs(foldertmp)
+    # create output dirs if needed
     try:
         os.makedirs(os.path.join(this_path, outfolder))
     except OSError:
@@ -59,17 +58,14 @@ def compressFiles(fileList, uuidkey, outfolder):
                     with open(fn, 'b+w') as f:
                         f.write(g.read())
 
-                # need to decompress file
-                datfile = os.path.join(foldertmp, os.path.basename(fn)[:-3])
-                with gzip.open(fn, 'r') as f_in, open(datfile, 'wb') as f_out:
-                    shutil.copyfileobj(f_in, f_out)
+                # need to decompress file and pass to zip
+                with gzip.open(fn, 'r') as f_in:
+                    myio = BytesIO(f_in.read())
                     print("Decomp " + os.path.basename(fn))
 
-                # and add file to zip
-                terrain_zip.write(datfile, os.path.basename(datfile), compress_type = zipfile.ZIP_DEFLATED)
+                    # and add file to zip
+                    terrain_zip.writestr(os.path.basename(fn)[:-3], myio.read(), compress_type = zipfile.ZIP_DEFLATED)
 
-            # remove cache (decompressed files)
-            shutil.rmtree(foldertmp)
     except Exception as ex:
         print("Unexpected error: {0}".format(ex))
         return False