Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Zip file into array of byte...

Thread view: 
gbattine - 29 Aug 2006 21:54 GMT
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
Moiristo - 29 Aug 2006 23:07 GMT
> Hi guys,
> i've a question for you.
[quoted text clipped - 5 lines]
> 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();
Jaakko Kangasharju - 30 Aug 2006 07:42 GMT
> It must be something like this (untested):
>
[quoted text clipped - 6 lines]
> 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;
}

Signature

Jaakko Kangasharju, Helsinki Institute for Information Technology
This space unintentionally left blank

Babu Kalakrishnan - 30 Aug 2006 08:03 GMT
>>It must be something like this (untested):
>>
[quoted text clipped - 19 lines]
>   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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.