473,662 Members | 2,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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))

Jul 5 '06 #1
1 6399
In <11************ *********@m79g2 000cwm.googlegr oups.com>, Ritesh Raj
Sarraf wrote:

I have some remarks on exception handling.
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")
`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(sSourc eDir)
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.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( )
Poor choice of names IMHO. I expect `filename` to be a file name, i.e. a
string and `files_to_compr ess` 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1137
by: Mark Wall | last post by:
If I have a UNIX compressed text file 'python.txt.Z' with contents: "Python " How do I read this data without uncompressing the file? Thank you, Mark
8
8830
by: Dennis Hotson | last post by:
Hi, I'm trying to write a function that adds a file-like-object to a compressed tarfile... eg ".tar.gz" or ".tar.bz2" I've had a look at the tarfile module but the append mode doesn't support compressed tarfiles... :( Any thoughts on what I can do to get around this?
6
3094
by: ad | last post by:
I have a compressed file in client. How could we send this compressed file (.zip) to server by web service?
8
2772
by: robert | last post by:
Hello, I want to put (incrementally) changed/new files from a big file tree "directly,compressed and password-only-encrypted" to a remote backup server incrementally via FTP,SFTP or DAV.... At best within a closed algorithm inside Python without extra shell tools. (The method should work with any protocol which allows somehow read, write & seek to a remote file.) On the server and the transmission line there should never be...
2
5145
by: pmlane2001 | last post by:
I have a PHP SOAP XML file size problem that I was wondering if anyone has seen before. I have an XML file that when I put it through my PHP script with 270 lines (13,082 KB) it works fine. If I make it 271 lines (13,112 KB), it causes the script to end with a soap fault. I've tried adding substance to the 270 lines to make the file bigger with out adding lines, but it didn't fail. I've installed everything on a second development box...
1
2120
by: prince.matt | last post by:
Hi, I am using GZipStream to compress a simple text file approx 24MB in size. I am finding that the destination "compressed" file is several MB larger than the source file (31MB). The code is as follows: Dim sourcefile As FileStream = File.OpenRead(inFileName) Dim destFile As FileStream = File.Open(outFilename, FileMode.Create, FileAccess.Write)
1
10201
by: sedwick | last post by:
I'm using .NET 2.0.50727 in VS 2005 Pro, ENU Service Pack 1 (KB926601). I've been experimenting with the System.IO.Compression classes GZipStream and DeflateStream, and I found it interesting to note that they'll create different-sized compressed files depending on the procedure you use to take in the source file's data. With one, almost every file I feed them for compression ends up significantly bigger. That code is below, almost verbatim...
4
6329
by: PRR | last post by:
i have a xml file ..around 200kb (max) .. i compressed it .. using GZipStream class. Now i wanna encrypt it.... Is it right? or the other way round... First encrypt and then compress.... Also i wanna know some good encrytion class in system.security.cryprto... i zeroed on Rijndael, http://msdn2.microsoft.com/en-us/library/system.security.cryptography.rijndael.aspx the data is impt... though not THAT impt.. still i wanna go with wat can...
1
1603
by: =?Utf-8?B?S2V5QmVl?= | last post by:
- YOU CAN PROTECT ZIPPED COMPRESSED FOLDER WITH A PASSWORD (PW). - YOU MUST PROVIDE THE PW BEFORE THE FILE CAN BE EXTRACTED. - HOW CAN YOU EXTRACT THE FILE IF YOU FORGOT THE PW ?
10
2665
by: Mark | last post by:
I used pickle and found the file was saved in text format. I wonder whether anyone is familiar with a good compact off-the-shelf module available that will save in compressed format... or maybe an opinion on a smart approach for making a custom one? Appreciate it! I'm a bit of a n00b but have been looking around. I found a serialize.py but it seems like overkill. Mark
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8343
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.