Home arrow static arrow Java Programming [Archive] - problem with file zipping!
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - problem with file zipping!
This topic has 19 replies on 2 pages.    1 | 2 | Next »

Posts:155
Registered: 6/9/03
problem with file zipping!  
Jul 19, 2004 11:20 AM



 
I have problem with my zip files. all that i need is a simple zipping of files. here's is what i am doing

//this mehod takes 2 arugments, first argument is the file which is to be zipped and the second argument is the name of the zip file
public void makeZIP(File fileToZip, String zfileName) throws IOException{
try{
byte[] buff = new byte[BUF_SIZE];//BUF_SIZE =128
int cnt=0;
FileInputStream swcConvFis = new FileInputStream(fileToZip);
ZipEntry swcConvZipEntry = new ZipEntry(zfileName);
ZipOutputStream swcConvZipOutputStream = new ZipOutputStream(swcConvFos);
swcConvZipOutputStream.putNextEntry(swcConvZipEntry);
while((cnt = swcConvFis.read(buff,0,BUF_SIZE)) != -1){
swcConvZipOutputStream.write(buff,0,cnt);
}
swcConvFis.close();
swcConvZipOutputStream.closeEntry();
}catch(NullPointerException npe){npe.printStackTrace();}
}

whis code is a part of the servlet code that has to send the zipped file to the applet. What's happening is i am able to zip the files. but when i try to open the zip file it gives me a an error window saying " cannot open file: it does not appear to be valid archive."

ofcourse the file is zipped on a linux machine and i am trying to open it in windows...it think this shouldn't be a problem though!!

please somebody throw some light on this. i have searched the archives but not helpful sofar.

thanks
sri
 

Posts:3,055
Registered: 18/06/98
Re: problem with file zipping!  
Jul 19, 2004 11:28 AM (reply 1 of 19)



 
Sometimes you can make something wrong when saving the files.
a) Check with a hex editor the binary contents of the generated zip file.
- if the generated zip file does not begins with "PK" (50 4B in hex) you're in trouble. Probably you've not saved the correct zipped data.
- if there are lots of "?" characters (3F), or lots of sequences 0D 0A, you probably saved the data using a "text" stream, not a "binary" stream.
b) Try some tool like the unzip.exe (you can get its source in www.info-zip.org)
unzip -t your_generated_zip_file.zip
If unzip barfs with your zip file, the format is incorrect (maybe something was not saved by your routine).
 

Posts:11,200
Registered: 7/22/99
Re: problem with file zipping!  
Jul 19, 2004 11:31 AM (reply 2 of 19)



 
Where does swcConvFos come from? And where do you close swcConvZipOutputStream?
 

Posts:18,384
Registered: 21.03.00
Re: problem with file zipping!  
Jul 19, 2004 11:39 AM (reply 3 of 19)



 
Hi,

Here is a complete example of how to write a jar-file with entries (but I removed the code for the manifest). I think you will able to alter the code so it stores zip-files instead of jar. I'm not sure, but I think you have to write the crc for zips as well.

import java.io.*;import java.util.jar.*;import java.util.zip.*; public class Test {     public static void main(String[] args) throws Exception {         FileOutputStream fileOut = new FileOutputStream("example.jar");        BufferedOutputStream bufferedOut = new BufferedOutputStream(fileOut);        JarOutputStream jar = new JarOutputStream(bufferedOut);         byte[] data = "First entry data".getBytes();        JarEntry entry = new JarEntry("Somedata.txt");        CRC32 crc = new CRC32();        crc.reset();        crc.update(data);        entry.setSize(data.length);        entry.setCrc(crc.getValue());        jar.putNextEntry(entry);        jar.write(data);        jar.closeEntry();         jar.finish();        jar.flush();        jar.close();    }} 
 

Posts:18,384
Registered: 21.03.00
Re: problem with file zipping!  
Jul 19, 2004 11:40 AM (reply 4 of 19)



 
Hi,

Let me now if it doesn't work the same way with zips.

/Kaj
 

Posts:155
Registered: 6/9/03
Re: problem with file zipping!  
Jul 19, 2004 11:43 AM (reply 5 of 19)



 
if(optionP.equals("-p")){
swcConvFiles = new File("swcConversionFiles.zip");
swcConvFos = new FileOutputStream(swcConvFiles);
userfiles = dir.listFiles();
for(int iter=0;iter<userfiles.length;iter++){
File temp = userfiles[iter];
String tempName = temp.getName();
if(tempName.endsWith("swc.swc")){
makeZIP(temp,tempName); //here i am calling the makeZIP method
}
}
swcConvFos.flush();
swcConvFos.close();

this piece of code goes before the previously posted makeZIP method....
this code recursively zips the files one-by-one that endwith swc.swc extension into the file names "swcConversionFiles.zip"

and when i tried to unzip the file in linux this is what it gives me

Archive: swcConversionFiles.zip
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
unzip: cannot find zipfile directory in one of swcConversionFiles.zip or
swcConversionFiles.zip.zip, and cannot find swcConversionFiles.zip.ZIP, period.
 

Posts:18,384
Registered: 21.03.00
Re: problem with file zipping!  
Jul 19, 2004 11:47 AM (reply 6 of 19)



 
Hi,

Hi, I just tested to re write my code to store zip-files and it works like a charm.

import java.io.*;import java.util.jar.*;import java.util.zip.*; public class Test {     public static void main(String[] args) throws Exception {         FileOutputStream fileOut = new FileOutputStream("example.zip");        BufferedOutputStream bufferedOut = new BufferedOutputStream(fileOut);        ZipOutputStream zip = new ZipOutputStream(bufferedOut);         byte[] data = "First entry data".getBytes();        ZipEntry entry = new ZipEntry("Somedata.txt");        CRC32 crc = new CRC32();        crc.reset();        crc.update(data);        entry.setSize(data.length);        entry.setCrc(crc.getValue());        zip.putNextEntry(entry);        zip.write(data);        zip.closeEntry();         zip.finish();        zip.flush();        zip.close();    }}


/Kaj
 

Posts:11,200
Registered: 7/22/99
Re: problem with file zipping!  
Jul 19, 2004 11:51 AM (reply 7 of 19)



 
You fail to close the zip file properly. You need to call ZipOutputStream.finish() or .close() so that the last bits of data that are required by the zip file format are written to the stream.

You need to redo your logic a bit, I would make the makeZIP method acceps an array of Files rather than just one and pass the full "userfiles" array to it. Then the method would make a zip file that contains all the specified files.

You can filter the output of dir.listFiles() using a FileFilter. http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileFilter.html
 

Posts:155
Registered: 6/9/03
Re: problem with file zipping!  
Jul 19, 2004 12:17 PM (reply 8 of 19)



 
jsalonen,
it works fine now, i just added a additonal
swcConvZipOutputStream.finish(); // which is a ZipOutputStream
but the second part of it is still not working...(i.e) the downloading part. the final zipped file is downloaded to the client applet by the servlet using the following code:

//input argument file will have swcConversionFiles.Zip files in this case
public void sendFile(File file) throws IOException{
//outputToApplet.writeUTF("sendFile file name "+file);
//String line="";
byte[] buff = new byte[BUF_SIZE];
int cnt = 0;
//byte[] eof = new byte[10];

FileInputStream fromFile = new FileInputStream(file);
try{
while((cnt = fromFile.read( buff )) > -1 ) {
outputToApplet.write( buff, 0, cnt );
}
}finally{
fromFile.close();
}

this is downloading the file at the specified location on to the client machine but it gives the same zip error as when i try to open it!!

and one more thing is your idea of extracting the files using a filter looks good. please tell me how to use this feature with a small example.

thaks
sri

 

Posts:11,200
Registered: 7/22/99
Re: problem with file zipping!  
Jul 19, 2004 12:33 PM (reply 9 of 19)



 
What is outputToApplet?

Are you sure that the problem is not in the applet's reading the sent data?

and one more thing is your idea of extracting the files using a
filter looks good. please tell me how to use this feature with a
small example.

There's an example here that accepts only directories and weeds out everything else:
    // This filter only returns directories    FileFilter fileFilter = new FileFilter() {        public boolean accept(File file) {            return file.isDirectory();        }    };    files = dir.listFiles(fileFilter);
http://javaalmanac.com/egs/java.io/GetFiles.html
 

Posts:155
Registered: 6/9/03
Re: problem with file zipping!  
Jul 19, 2004 12:43 PM (reply 10 of 19)



 
the outputToApplet is the ObjectOutputStream object.

at the applet's reading end this is what i am doing

ObjectInputStream intputFromServlet = new ObjectInputStream(connect.getInputStream());
//code to read the output files from the servlet
byte[] buff = new byte[BUF_SIZE];
int cnt = 0;
ArrayList getOutputFilesFromServlet = (ArrayList) inputFromServlet.readObject(); // at the servlet end i am writing outputToApplet.writeObject(send2Files), send2Files is an ArrayList object that has one .txt file and another .zip file

System.out.println("total no.of files "+getOutputFilesFromServlet.size()); //shows 2
Iterator getOutputFilesFromServletIterator = getOutputFilesFromServlet.iterator();
while(getOutputFilesFromServletIterator.hasNext()){
File file = (File)getOutputFilesFromServletIterator.next();
String fname = file.getName();
System.out.println("received "+fname);//shows correct file names
try{
File nfile = new File(getOfile.getParent()+System.getProperty("file.separator")+fname);//puts the files in the requested directory on the client machine
FileOutputStream toFile = new FileOutputStream(nfile);
//reading the files uploaded by the applet
while( (cnt = dump.read( buff )) > -1 ){
if(cnt<BUF_SIZE){
toFile.write( buff, 0, cnt );
toFile.close();
break;
}
toFile.write( buff, 0, cnt );

}

}catch(Exception e){e.printStackTrace();}
System.out.println("finished reading all files from the servlet....");
}//while(it.next)

not only the zip file even the other text file is not downloaded properly....(i.e) it shows some junk characters instead of the actual file context. so maybe there is mistake in the way that i am reading the files...but what i am doing at applet's end is just a mirror reflection of what i am doing at the servlet's end. you got me!! hope i am not confusing!
any suggestions!!

 

Posts:155
Registered: 6/9/03
Re: problem with file zipping!  
Jul 19, 2004 12:50 PM (reply 11 of 19)



 
sorry in my previous code i forgot to replace varibale dump with the inputFromServlet. the variable dump is nothing but inputFromServlet, I have renamed it to make it clear to you
 

Posts:11,200
Registered: 7/22/99
Re: problem with file zipping!  
Jul 19, 2004 12:58 PM (reply 12 of 19)



 
I don't think you can't send data like that; the object output/input streams are meant for sending complete objects and you seem to be using it to transmit something totally different. This looks very suspicious.

if(cnt<BUF_SIZE){
toFile.write( buff, 0, cnt );
toFile.close();
break;
}

What is this for, why do close the file and exit the loop when you happen to read BUF_SIZE bytes, which may well happen long before the actual end of the stream?
 

Posts:155
Registered: 6/9/03
Re: problem with file zipping!  
Jul 19, 2004 1:13 PM (reply 13 of 19)



 
well, i used same piece of code at the servlet end to read the input from applet. my applet also uploads a set of files. so this works!!

and this piece is to demarcate between the end of files
if(cnt<BUF_SIZE){
toFile.write( buff, 0, cnt );
toFile.close();
break;
}

since the input/output stream consists data from different files and if i don't have this condition all the data is written onto one file. which i don't want.
 

Posts:11,200
Registered: 7/22/99
Re: problem with file zipping!  
Jul 19, 2004 1:55 PM (reply 14 of 19)



 
well, i used same piece of code at the servlet end to read the
input from applet. my applet also uploads a set of files. so this
works!!

then in that case maybe it's ok..

and this piece is to demarcate between the end of files

I think it is not doing what you think it does. The general contract of the read(byte[]) method is that it reads as much as can be read without the stream blocking (needing to wait for the network or file system or whatever) and returns the number of bytes actually read; it's quite common for it to be unable to fill the byte array.

So you need some other way to tell where one file ends and the other begins. One way would be transmitting the file size before the data: this way the receiving side could keep a counter on the number of bytes it has got and know when to stop reading.

Are you sure you need to do things this complicated, do you have to send everything over one single network connection?
 
This topic has 19 replies on 2 pages.    1 | 2 | Next »