473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Get the entire file in a variable - error

H!

I'm using a database and now I want to compress a file and put it into
the database.

So I'm using gzip because php can open the gzip file's.
The only problem is saving the file into the database.

The function below does this:
- gzip the file [oke]
- get all the bytes with tst.getvalue() [error]
I only get the first line.

I have the same problem when I try it with file.open(), .read().

"how to get all the binary data in a variable to put that in a database
LONG field?"

Thank's

def compressFILE(sid,filedata):
tst = StringIO.StringIO()

#tmp
zip = gzip.GzipFile(str(sid)+'.gz','w',5,tst)
zip.write(filedata)

#put every byte in a database
print tst.getvalue()

zip.close()
tst.close()

return

Jul 19 '05 #1
5 1372
It's not clear to me what you mean by "the first line" (gzip does not
output a file composed of lines, its output is byte-oriented).

Printing tst.getvalue() is probably not a very useful thing to do, since
it won't do anything useful when the output is a terminal, and it will
add an extra newline if you are redirecting to a file.

At least when close()ing the GzipFile before looking at the StringIO
instance's value, I get some bytes that gunzip just fine, giving the
original string.

Here's my interactive session:
import gzip
import StringIO
io = StringIO.StringIO()
z = gzip.GzipFile("test.gz", "w", 5, io)
z.write("""\ ... Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
... [GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
... Type "help", "copyright", "credits" or "license" for more information.
... """) z.close()
from os import popen
popen("gunzip -dc", "w").write(io.getvalue()) Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.


I don't know anything about your database or its "LONG field".
Depending on the database software there could be additional problems
with embedded NULs, for instance.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFCXoThJd01MZaTXX0RAgLqAJ9OZ6OEpJwB3wVUq5IlV2 SFx2Zh7gCggdR4
N4njFJTpuOJ37GOuVhF8nRU=
=f5At
-----END PGP SIGNATURE-----

Jul 19 '05 #2
I mean it like this.

I must have a variable that includes a file (in this case a .gz file)
for putting that in a database. (never null)

Thanks,

Jul 19 '05 #3

What kind of DB? Maybe the field type you're hunting for is a BLOB? Or
perhaps you need to be formatting the string differently than it's
entered (like php's addslashes() or something)?
--
Pokerface:: Posted from Tactical Gamer - http://www.TacticalGamer.com ::

Jul 19 '05 #4
In the future it REALLY helps if you post the code that
you used to open and read the file. Otherwise we have
to "guess" what you may of done.

That said, I'll try to guess:

1) Your code shows that you instantiate GzipFile class with
mode "w". I don't know what your data looks like but
normally you would want to use "wb" to handle binary
data.

2) You also need to do the same thing when you try to read
the file:

fp=file(filename, 'rb')

When you don't the first read will read until it encounters
binary bytest that look like a EOF and quit. When you open
with "b" mode it reads the number of bytes that the
filesytem has stored for the file.

Hope info helps.

Larry Bates
ma*****@gamecreators.nl wrote:
H!

I'm using a database and now I want to compress a file and put it into
the database.

So I'm using gzip because php can open the gzip file's.
The only problem is saving the file into the database.

The function below does this:
- gzip the file [oke]
- get all the bytes with tst.getvalue() [error]
I only get the first line.

I have the same problem when I try it with file.open(), .read().

"how to get all the binary data in a variable to put that in a database
LONG field?"

Thank's

def compressFILE(sid,filedata):
tst = StringIO.StringIO()

#tmp
zip = gzip.GzipFile(str(sid)+'.gz','w',5,tst)
zip.write(filedata)

#put every byte in a database
print tst.getvalue()

zip.close()
tst.close()

return

Jul 19 '05 #5
On 14 Apr 2005 07:37:11 -0700, ma*****@gamecreators.nl wrote:
H!

I'm using a database and now I want to compress a file and put it into
the database.

So I'm using gzip because php can open the gzip file's.
The only problem is saving the file into the database.

The function below does this:
- gzip the file [oke]
- get all the bytes with tst.getvalue() [error]
I only get the first line.

I have the same problem when I try it with file.open(), .read().

"how to get all the binary data in a variable to put that in a database
LONG field?"

Thank's

def compressFILE(sid,filedata):
tst = StringIO.StringIO()

#tmp
zip = gzip.GzipFile(str(sid)+'.gz','w',5,tst)
According to the docs, it will convert your 'w' into 'wb'. That
shouldn't be your problem. However just because I'm paranoid doesn't
mean someone isn't out to get you, so change it to 'wb' anyway. It's
an extremely good habit to put the 'b' on whenever you are reading or
writing binary data.
zip.write(filedata)

#put every byte in a database
print tst.getvalue()
Here's your problem, being over-eager. Compressors that allow you to
feed them their input a spoonful at a time will maintain a large
amount of "state" and won't finish the job until you call their
close() method. Just like if you have a process writing a file to
disk, a second process trying to read the file before the first
process closes it is not in general guaranteed to see *any* of the
file's content, let alone all of it.

zip.close()
tst.close()


OK, now you can inspect the contents of "tst", write it to your
database, and then (and only then) worry about BLOBs and embedded
nulls and other nasties.

HTH,

John
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jody Burgess | last post by:
Hi; I am writing my first python program and would like to know how to change stdout to refer to my default printer or any other printer on my network. The other question is, Is there an API set...
5
by: Scott Tilton | last post by:
I am having a terrible time getting this to work. I am hoping someone out there can help me with very specific code examples. I am trying to get the linked tables in my Access 97 database to be...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
4
by: owolablo | last post by:
I'm trying to make a subprogram, Code: void file2String(istream& in, string& theFile) that can read an entire file into a string variable, including line breaks and i have no idea what im...
4
by: Fregas | last post by:
There is an old site that i'm having to maintain in classic asp. For some reason, the original developers didn't belive in validation, so there are many places where if the user puts in invalid...
0
by: Alexis Boutillier | last post by:
Timothy Grant a écrit : As you can see in my last response, this problem is not linked to the type of error, If I "raise" a SystemError instead of creating a SyntaxError I still can't access...
1
by: Sunny | last post by:
Hi, Can someone tell me how to get the content of entire xml file in Firefox? I am using this code to load the xml file. if (document.implementation && document.implementation.createDocument) {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.