By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,492 Members | 1,289 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,492 IT Pros & Developers. It's quick & easy.

compressed file ended before the logical end-of-stream was detected

P: n/a
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))

Jul 5 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
In <11*********************@m79g2000cwm.googlegroups. com>, Ritesh Raj
Sarraf wrote:

I have some remarks on exception handling.
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")
`zipfile` is part of the standard library. So it should be there. If it
isn't then you write the message and the program will run into a name
error exception because `zipfile` isn't imported then but you use it later
on.
try:
os.chdir(sSourceDir)
except:
#TODO: Handle this exception
pass
Don't do that! This catches every exception and ignores it. Write just a
comment instead that says something like `TODO: add proper exception
handling`.
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()
Poor choice of names IMHO. I expect `filename` to be a file name, i.e. a
string and `files_to_compress` is plural so one may expect a list or
another iterable.

This `import` wrapped into `try`/`except ImportError` idiom sprinkled all
over the source is a bit odd. Just do your imports and wrap the main
programs function into `try`/`except` if you want to catch the
`ImportError`\s. If you stop the program anyway this leads to much less
cluttered source code.

And it maybe would make sense to print the actual catched `IOError`\s too
because it may be a valueable information for the user *why* it failed.
For example because he has no rights or the file system is full.

Ciao,
Marc 'BlackJack' Rintsch
Jul 5 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.