Hi,
The program downloads the files from the internet and compresses them
to a single zip archive using compress_the_fi le().
Upon running syncer() which calls the decompress_the_ file(), the first
iteration succeeds. But upon second iteration, I get an IOError
exception with the message:
"compressed file ended before the logical end-of-stream was detected"
Any ideas why this happens ? I don't think it has anything to do with
bad packets from the internet.
Here's the code.
There are two functions:
1) compress_the_fi le() - This function takes files as an argument to it
and put all of them into a zip archive
def compress_the_fi le(zip_file_nam e, files_to_compre ss, sSourceDir):
'''Condenses all the files into one single file for easy
transfer'''
try:
import zipfile
except ImportError:
sys.stderr.writ e("Aieeee! module not found.\n")
try:
os.chdir(sSourc eDir)
except:
#TODO: Handle this exception
pass
try:
filename = zipfile.ZipFile (zip_file_name, "a")
except IOError:
#INFO By design zipfile throws an IOError exception when you
open
# in "append" mode and the file is not present.
filename = zipfile.ZipFile (zip_file_name, "w")
except:
#TODO Handle the exception
sys.stderr.writ e("\nAieee! Some error exception in creating zip
file %s\n" % (zip_file_name) )
sys.exit(1)
filename.write( files_to_compre ss, files_to_compre ss,
zipfile.ZIP_DEF LATED)
filename.close( )
My actual program iterates over a loop and sends files to this funtion
as it downloads from the internet.
2) decompress_the_ file() - This function takes archive file as an
argument and decompresses them.
The above function decompress_the_ file() is called by function syncer()
syncer(): This function tells decompress_the_ file() what type file it
is and at which path to decompress.
def syncer(install_ file_path, target_path, type=None):
'''Syncer does the work of syncing the downloaded files.
It syncs "install_file_p ath" which could be a valid file path
or a zip archive to "target_pat h'''
if type == 1:
try:
import zipfile
except ImportError:
sys.stderr.writ e("Aieeee! Module zipfile not found.\n")
sys.exit(1)
file = zipfile.ZipFile (install_file_p ath, "r")
for filename in file.namelist() :
try:
import file_magic
except ImportError:
sys.stderr.writ e("Aieeee! Module file_magic not
found.\n")
sys.exit(1)
data = open(filename, "wb")
data.write(file .read(filename) )
data.close()
#data = file.read(filen ame)
if file_magic.file (filename) == "applicatio n/x-bzip2":
decompress_the_ file(os.path.ab spath(filename) ,
target_path, filename, 1)
elif file_magic.file (filename) == "PGP armored data":
try:
shutil.copy(fil ename, target_path)
sys.stdout.writ e("%s file synced.\n" % (filename))
except shutil.Error:
sys.stderr.writ e("%s is already present.\n" %
(filename))