Q: Is there a simple python way to reoganize a zip?
In an attempt to simplify the conversion from our regular updates from a feature class in a FGDB to zipped shapefile for uploading into ArcGIS Online for users to download, I'm modifying the model

(converted to python snippets ). This sample, per the help Extract Data—Help | ArcGIS for Desktop
Extracts selected layers in the specified area of interest to a specific format and spatial reference. The extracted data is then written to a zip file. |
Caution:
This tool is intended primarily for use as part of a geoprocessing service. When using this tool as part of a geoprocessing service, copy the tool into a custom toolbox, edit the model, and reconfigure it as necessary. For step-by-step instructions on how to make, use, and configure a geoprocessing service using this tool, see Geoprocessing service example: Clip And Ship. |
Although it is meant to be used as a GP tool, I have been able to get my script to work, but the output it not AGOL friendly. That is, it adds another layer/folder in the zip structure... e.g. myfile.zip\zipfolder#\myshapefile.* This "zipfolder#" seems to be hard coded into the ExtractData_server. I know there are other scripts to open/read/write to a .zip, but this actually works nice, except for that added folder.
Is there a simple python way to reoganize a zip. If it requires an open-cursor-write-close like the samples I've seen, I may just go that route and skip the ExtractData command, but other than that extra layer, this custom version works great for my needs (just one shapefile to a zip)
I've testing in 10.2.2 and 10.3.1, Catalog and map (python command window, not as a tool yet)
outputted format:

would like the "zipfolder#" level removed.
my test script:
# Import arcpy module
import arcpy
import os
import time
from time import localtime
def myMsgs(message):
arcpy.AddMessage("{0}".format(message))
print("{0}".format(message) )
# Script arguments...
sourceGDB = arcpy.GetParameterAsText(0)
if not sourceGDB:
sourceGDB = r"d:\data\wc\Master_update.gdb"
sourceFDS = arcpy.GetParameterAsText(1)
if not sourceFDS:
sourceFDS = "DWCMasters"
myMsgs(sourceFDS)
sourceFDS = os.path.join(sourceGDB, sourceFDS)
myMsgs(sourceFDS)
GMULayers = arcpy.GetParameterAsText(2)
if not GMULayers:
GMULayers = os.path.join(sourceGDB, "GMULayers")
myMsgs(GMULayers)
# export as shape files...will create folder for pubYear
targetFolder = arcpy.GetParameterAsText(3)
if not targetFolder:
targetFolder = r"d:\_forAGOLdist"
pubYear = localtime().tm_year
targetFolder = os.path.join(targetFolder, ("{0}_test".format(pubYear)))
if not os.path.isdir(targetFolder):
os.mkdir(targetFolder)
myMsgs("Creating target folder: {0}".format(targetFolder))
else:
myMsgs("Output shapes will be written to: {0}".format(targetFolder))
Feature_Format = "Shapefile - SHP - .shp"
Raster_Format = '#'
Spatial_Reference = "Same As Input"
Custom_Spatial_Reference_Folder = ""
# subs
inSubsFC = os.path.join(GMULayers, "Subunits")
inSubFL = "Subunits_Layer"
Layers_to_Clip = inSubFL
Area_of_Interest = Layers_to_Clip
Output_Zip_File = os.path.join(targetFolder, "Subunits.zip")
arcpy.MakeFeatureLayer_management(inSubsFC,inSubFL)
arcpy.ExtractData_server(Layers_to_Clip, Area_of_Interest, Feature_Format, Raster_Format, Spatial_Reference, Custom_Spatial_Reference_Folder, Output_Zip_File)
# Spec Areas
inSAFC = os.path.join(sourceFDS, "SpecialAreas")
inSAFL = "SpecArea_Layer"
qryActive = "status = 'a'"
Layers_to_Clip = inSAFL
Area_of_Interest = Layers_to_Clip
Output_Zip_File = os.path.join(targetFolder, "SpecAreas.zip")
field_info = ("OBJECTID OBJECTID VISIBLE NONE;Shape Shape VISIBLE NONE;TYPE TYPE VISIBLE NONE;AREANAME AREANAME VISIBLE NONE;SHORTNAME SHORTNAME VISIBLE NONE;GMU GMU VISIBLE NONE;REGION REGION VISIBLE NONE;gisNotes gisNotes HIDDEN NONE;created_user created_user HIDDEN NONE;created_date created_date HIDDEN NONE;last_edited_user last_edited_user HIDDEN NONE;last_edited_date last_edited_date HIDDEN NONE;status status HIDDEN NONE;Shape_Length Shape_Length HIDDEN NONE;Shape_Area Shape_Area HIDDEN NONE;SqMi SqMi VISIBLE NONE")
arcpy.MakeFeatureLayer_management(inSAFC, inSAFL,qryActive,"#",field_info)
arcpy.ExtractData_server(Layers_to_Clip, Area_of_Interest, Feature_Format, Raster_Format, Spatial_Reference, Custom_Spatial_Reference_Folder, Output_Zip_File)I've found a couple py-zip type samples in my searches, but again, they all require the O-R-Wr-Cl operation, which will be ok, but then I would skip the command, since this is a simple shapefile to .zip I'm trying to do.
Thanks.
edit: added line 58, so output changed
more edits....lots of typos...must be getting tired.