# Path: image_filer/utils/zip.py
# def unzip(file):
#     """
#     Take a path to a zipfile and checks if it is a valid zip file
#     and returns...
#     """
#     files = []
#     # TODO: implement try-except here
#     zip = zipfile.ZipFile(file)
#     bad_file = zip.testzip()
#     if bad_file:
#         raise Exception('"%s" in the .zip archive is corrupt.' % bad_file)
#     infolist = zip.infolist()
#     print infolist
#     for zipinfo in infolist:
#         print "handling %s" % zipinfo.filename
#         if zipinfo.filename.startswith('__'): # do not process meta files
#             continue
#         thefile = SimpleUploadedFile(name=zipinfo.filename, content=zip.read(zipinfo))
#         files.append( (thefile, zipinfo.filename) )
#     zip.close()
#     return files


import os
from image_filer.utils.zip import unzip


def generic_handle_file(file, original_filename):
    """
    Handels a file, regardless if a package or a single file and returns 
    a list of files. can recursively unpack packages.
    """
    #print "entering generic_handle_file(file=%s, original_filename=%s)" % (file, original_filename)
    files = []
    filetype = os.path.splitext(original_filename)[1].lower()
    #print filetype
    if filetype=='.zip':

# Path: image_filer/utils/zip.py
# def unzip(file):
#     """
#     Take a path to a zipfile and checks if it is a valid zip file
#     and returns...
#     """
#     files = []
#     # TODO: implement try-except here
#     zip = zipfile.ZipFile(file)
#     bad_file = zip.testzip()
#     if bad_file:
#         raise Exception('"%s" in the .zip archive is corrupt.' % bad_file)
#     infolist = zip.infolist()
#     print infolist
#     for zipinfo in infolist:
#         print "handling %s" % zipinfo.filename
#         if zipinfo.filename.startswith('__'): # do not process meta files
#             continue
#         thefile = SimpleUploadedFile(name=zipinfo.filename, content=zip.read(zipinfo))
#         files.append( (thefile, zipinfo.filename) )
#     zip.close()
#     return files


# Path: image_filer/utils/files.py
import os
from image_filer.utils.zip import unzip



def generic_handle_file(file, original_filename):
    """
    Handels a file, regardless if a package or a single file and returns 
    a list of files. can recursively unpack packages.
    """
    #print "entering generic_handle_file(file=%s, original_filename=%s)" % (file, original_filename)
    files = []
    filetype = os.path.splitext(original_filename)[1].lower()
    #print filetype
    if filetype=='.zip':

        unpacked_files = unzip(file)