{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Example of how to make and use a ZipArchive\n",
    "ZipArchives are a powerful tool for collecting files and managing data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# first we import the ZipModels module.\n",
    "from pyMez.Code.DataHandlers.ZipModels import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# a new zip archive\n",
    "new_zip=ZipArchive()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Data_Archive_20181114_002.zip'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# it has a default name\n",
    "new_zip.path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# we can add a file to the zip archive\n",
    "file_to_add=r\"./Zip_Example_Files/WR15_isolation.s2p\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# just provide the path to the file to add\n",
    "new_zip.add_file(file_to_add)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['WR15_isolation.s2p']"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# now the files are listed in the attribute files\n",
    "new_zip.files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# we can add files of any type\n",
    "new_zip.add_file(r\"./Zip_Example_Files/WR15_Compare_Corrected_Loads.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['WR15_isolation.s2p', 'WR15_Compare_Corrected_Loads.png']"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_zip.files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# we can add all the files in a directory\n",
    "new_zip.add_all_from_directory(r\".\\Zip_Example_Files\\Mulitple_Files\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['WR15_isolation.s2p',\n",
       " 'WR15_Compare_Corrected_Loads.png',\n",
       " 'Thru_WR15_20180215_001.s2p',\n",
       " 'Thru_WR15_20180215_002.s2p',\n",
       " 'Thru_WR15_20180215_003.s2p',\n",
       " 'Thru_WR15_20180215_004.s2p']"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_zip.files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# we can create a string and add it as a new file\n",
    "new_zip.write_string(data_string=\"A new fake file\",archive_file_name=\"string.txt\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# lets save the archive\n",
    "new_zip.save(os.path.join(os.getcwd(),r\"Zip_Example_Files\\ZipArchive_Test_01.zip\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['WR15_isolation.s2p',\n",
       " 'WR15_Compare_Corrected_Loads.png',\n",
       " 'Thru_WR15_20180215_001.s2p',\n",
       " 'Thru_WR15_20180215_002.s2p',\n",
       " 'Thru_WR15_20180215_003.s2p',\n",
       " 'Thru_WR15_20180215_004.s2p',\n",
       " 'string.txt']"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_zip.files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# now we can extract all of the files to a directory\n",
    "new_zip.extract_all(destination_directory=r\"Zip_Example_Files\\Extracted_Files\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# now we close it and it goes away. Note, because of the way zip files work a copy will be saved under the default name.\n",
    "# To change the default name set the options directory, specfic_descriptor, general_descriptor"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
