R
Ritesh Raj Sarraf
Hi,
The program downloads the files from the internet and compresses them
to a single zip archive using compress_the_file().
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_file() - This function takes files as an argument to it
and put all of them into a zip archive
def compress_the_file(zip_file_name, files_to_compress, sSourceDir):
'''Condenses all the files into one single file for easy
transfer'''
try:
import zipfile
except ImportError:
sys.stderr.write("Aieeee! module not found.\n")
try:
os.chdir(sSourceDir)
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.write("\nAieee! Some error exception in creating zip
file %s\n" % (zip_file_name))
sys.exit(1)
filename.write(files_to_compress, files_to_compress,
zipfile.ZIP_DEFLATED)
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_path" which could be a valid file path
or a zip archive to "target_path'''
if type == 1:
try:
import zipfile
except ImportError:
sys.stderr.write("Aieeee! Module zipfile not found.\n")
sys.exit(1)
file = zipfile.ZipFile(install_file_path, "r")
for filename in file.namelist():
try:
import file_magic
except ImportError:
sys.stderr.write("Aieeee! Module file_magic not
found.\n")
sys.exit(1)
data = open(filename, "wb")
data.write(file.read(filename))
data.close()
#data = file.read(filename)
if file_magic.file(filename) == "application/x-bzip2":
decompress_the_file(os.path.abspath(filename),
target_path, filename, 1)
elif file_magic.file(filename) == "PGP armored data":
try:
shutil.copy(filename, target_path)
sys.stdout.write("%s file synced.\n" % (filename))
except shutil.Error:
sys.stderr.write("%s is already present.\n" %
(filename))
The program downloads the files from the internet and compresses them
to a single zip archive using compress_the_file().
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_file() - This function takes files as an argument to it
and put all of them into a zip archive
def compress_the_file(zip_file_name, files_to_compress, sSourceDir):
'''Condenses all the files into one single file for easy
transfer'''
try:
import zipfile
except ImportError:
sys.stderr.write("Aieeee! module not found.\n")
try:
os.chdir(sSourceDir)
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.write("\nAieee! Some error exception in creating zip
file %s\n" % (zip_file_name))
sys.exit(1)
filename.write(files_to_compress, files_to_compress,
zipfile.ZIP_DEFLATED)
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_path" which could be a valid file path
or a zip archive to "target_path'''
if type == 1:
try:
import zipfile
except ImportError:
sys.stderr.write("Aieeee! Module zipfile not found.\n")
sys.exit(1)
file = zipfile.ZipFile(install_file_path, "r")
for filename in file.namelist():
try:
import file_magic
except ImportError:
sys.stderr.write("Aieeee! Module file_magic not
found.\n")
sys.exit(1)
data = open(filename, "wb")
data.write(file.read(filename))
data.close()
#data = file.read(filename)
if file_magic.file(filename) == "application/x-bzip2":
decompress_the_file(os.path.abspath(filename),
target_path, filename, 1)
elif file_magic.file(filename) == "PGP armored data":
try:
shutil.copy(filename, target_path)
sys.stdout.write("%s file synced.\n" % (filename))
except shutil.Error:
sys.stderr.write("%s is already present.\n" %
(filename))