Zip file into array of byte...

G

gbattine

Hi guys,
i've a question for you.
I'm developing a jsf application and i've performed upload function.
Now i want to upload a zip file and put it into a blob field of a mysql
table.
I need some code to convert the zip file into an array of byte to put
it into a blob and to perform inverse function.
Can you post me code,idea or link?
Thanks very much
 
M

Moiristo

gbattine said:
Hi guys,
i've a question for you.
I'm developing a jsf application and i've performed upload function.
Now i want to upload a zip file and put it into a blob field of a mysql
table.
I need some code to convert the zip file into an array of byte to put
it into a blob and to perform inverse function.
Can you post me code,idea or link?
Thanks very much

It must be something like this (untested):


File f = new File("zipfile.zip");

byte[] zipData = new byte[f.length()];

// Try/catch omitted
InputStream is = new FileInputStream(f);
is.read(zipData, 0, f.length());
is.close();

PreparedStatement ps = myConnection.prepareStatement("INSERT INTO
ZipTable VALUES ?");
ps.setBinaryStream(1,new ByteArrayInputStream(zipData),zipData.length);
ps.executeUpdate();
 
J

Jaakko Kangasharju

Moiristo said:
It must be something like this (untested):


File f = new File("zipfile.zip");

byte[] zipData = new byte[f.length()];

// Try/catch omitted
InputStream is = new FileInputStream(f);
is.read(zipData, 0, f.length());
is.close();

When you need to read a specific amount of bytes, you always need to
do it in a loop because read() does not need to read the full amount
that you're asking it, i.e., something like

// error and EOF handling omitted
int len = f.length();
int offset = 0;
while (len > 0) {
int n = is.read(zipData, offset, len);
offset += n;
len -= n;
}
 
B

Babu Kalakrishnan

Jaakko said:
It must be something like this (untested):


File f = new File("zipfile.zip");

byte[] zipData = new byte[f.length()];

// Try/catch omitted
InputStream is = new FileInputStream(f);
is.read(zipData, 0, f.length());
is.close();


When you need to read a specific amount of bytes, you always need to
do it in a loop because read() does not need to read the full amount
that you're asking it, i.e., something like

// error and EOF handling omitted
int len = f.length();
int offset = 0;
while (len > 0) {
int n = is.read(zipData, offset, len);
offset += n;
len -= n;
}


Alternately, if you're dealing with disk files only, one could create a
RandomAccessFile object and use its readFully() method. For other types
of streams, you can use the same method after wrapping the stream into a
DataInputStream.

BK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,268
Latest member
AshliMacin

Latest Threads

Top