Home » Category » Web Tier APIs

Web Tier APIs: ZipOutputStream and files with whitespaces

300| Fri, 21 Sep 2007 18:22:00 GMT| jerry_garciaa| Comments (3)
I have a page that will generate a zip file from the users selected files(checkbox).
Here is the code:

if(counter >= MAXFILES){counter=0;}

outFilename = "C:\\ZIP\\OK"+counter+".zip";

counter++;

ZipOutputStream outfile = new ZipOutputStream(new FileOutputStream(outFilename));

// Compress the files

for (int i=0; i<filenames.length; i++) {

FileInputStream in = new FileInputStream(PATH+filenames[i]);

// Add ZIP entry to output stream.

outfile.putNextEntry(new ZipEntry(PATH+filenames[i]));

// Transfer bytes from the file to the ZIP file

int len;

while ((len = in.read(buf)) > 0) {

outfile.write(buf, 0, len);

}

// Complete the entry

outfile.closeEntry();

in.close();

}

// Complete the ZIP file

outfile.close();

} catch (IOException e) {

}

}%>

It works fine, except that when the user selects a file that contains whitespaces in the name ( file Name.txt). When a file with naming conventions as above is added to the FileInputStream the resulting zip file is corrupted(winzip wont open it)[file does not appear to be a valid zip archive]is what the winzip error is.

Anyone know why whitespaced file names will cause this? Is there a solution?

Thanks in advance!!

Keywords & Tags: zipoutputstream, files, whitespaces, web, tier, apis

URL: http://java.itags.org/java-web-tier-apis/79640/
 
«« Prev - Next »» 3 helpful answers below.
any takers?

jerry_garciaa | Sun, 15 Jul 2007 17:52:00 GMT |

Not sure if this will help, but you might want to do a check on the filenames first and if you find a white space in the filename replace the space with the ASCII representation of a space (%20) without the params...I haven't tested it or anything but it might be a

dapanther99a | Sun, 15 Jul 2007 17:52:00 GMT |

[nobr] I have determined that the request.getParameter value is what is getting truncated:

String[] fileNames = request.getParameterValues("cb");

int count = 0;if (fileNames != null) { count = fileNames.length; }

for (int i = 0; i < count; i++) { out.println(fileNames[i]+ "<br>"); }

The preceding code gives me:

file1.txt(actual name is file1.txt)

file2.txt(actual name is file2.txt)

file3.txt(actual name is file3.txt)

Copy(actual name is Copy of file3.txt)

So I tried this:

String[] fileNames = request.getParameterValues("cb");

int count = 0;if (fileNames != null) { count = fileNames.length; }

for (int i = 0; i < count; i++) { out.println(fileNames[i].replaceAll(" ","%20") + "<br>"); }

Which doesnt work.

Any ideas?[/nobr]

jerry_garciaa | Sun, 15 Jul 2007 17:52:00 GMT |

Web Tier APIs Hot Answers

Web Tier APIs New questions

Web Tier APIs Related Categories