473,799 Members | 3,339 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(si d,filedata):
tst = StringIO.String IO()

#tmp
zip = gzip.GzipFile(s tr(sid)+'.gz',' w',5,tst)
zip.write(filed ata)

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

zip.close()
tst.close()

return

Jul 19 '05 #1
5 1389
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.String IO()
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.g etvalue()) 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)

iD8DBQFCXoThJd0 1MZaTXX0RAgLqAJ 9OZ6OEpJwB3wVUq 5IlV2SFx2Zh7gCg gdR4
N4njFJTpuOJ37GO uVhF8nRU=
=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(filenam e, '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*****@gamecre ators.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(si d,filedata):
tst = StringIO.String IO()

#tmp
zip = gzip.GzipFile(s tr(sid)+'.gz',' w',5,tst)
zip.write(filed ata)

#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*****@gamecre ators.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(si d,filedata):
tst = StringIO.String IO()

#tmp
zip = gzip.GzipFile(s tr(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(filed ata)

#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
2602
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 of classes that allow me to interact with network devices. In other words, if I have a path and filename inside of a string variable in python, how do I send the file to the printer?? Thanks in advance
5
3307
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 updated whenever the database opens. I need to have them updated based on an INI file that resides in the same directory as the current database. I do not need or want the ability to open a dialog box to pick the location. I simply want it to...
0
3944
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. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
2
5049
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 folder structure and naming conventions and then send a Net send message to those users telling them to rectify. The information I want to get is when you select the file/folder and then: Properties -> Security Tab -> Advanced Button -> Owner Tab ->...
1
6510
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" setting to be "E_ALL", notices are still not getting reported. The perms on my file are 664, with owner root and group root. The php.ini file is located at /usr/local/lib/php/php.ini. Any ideas why the setting does not seem to be having an effect? ...
4
4303
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 doing. I'm using the GNU g++ compiler on Fedora 4 Linux Distribution. Thanks in advance for the help
4
1853
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 data, asp/vbscript throws a big sql server error. In development, on my windows XP box, I can see this error (basically the field is not large enough in sql server for the text they are trying to enter.) However, when this error occurs on the...
0
1866
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 variable defined before the error. -- Boutillier Alexis Methodology engineer
1
2244
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) { xmlDoc = document.implementation.createDocument("","",null); xmlDoc.async="false"; var isLoaded = xmlDoc.load("allcounties_3.xml"); if (isLoaded == true) {
0
10484
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10228
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
10027
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...
0
9072
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
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
6805
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
5463
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...
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.