Hello,
Wondering if anyone could point me to the right way of streaming a .zip file:
my goal is to stream a zipped version of the index. I zip up the index files I
get from calling IndexCommit#getFileNames, and then attempt to stream using a
custom handler with the following in handleRequestBody:
File targetZip = new File({pathToMyZipFile}));
ModifiableSolrParams params = new ModifiableSolrParams(req.getParams());
params.set(CommonParams.WT, "raw");
req.setParams(params);
ContentStreamBase content = new ContentStreamBase.FileStream(targetZip);
content.setContentType("application/zip");
rsp.add(RawResponseWriter.CONTENT, content);
I'm able to open the zip file (targetZip) and see the contents. However, I'm
having trouble getting there on the receiving end, where I try reading the file
by doing essentially the same as below:
BufferedReader reader = null;
BufferedWriter writer = null;
try {
InputStream is = content.getStream();
reader = new BufferedReader(new InputStreamReader(is,
ContentStreamBase.DEFAULT_CHARSET));
File temp = File.createTempFile("myFile", ".zip");
writer = new BufferedWriter(new OutputStreamWriter(new
FileOutputStream(temp), ContentStreamBase.DEFAULT_CHARSET));
String currentLine;
while((currentLine = reader.readLine()) != null) {
writer.write(currentLine);
writer.newLine();
}
writer.close();
} catch (Exception x) {
x.printStackTrace();
} finally {
IOUtils.closeQuietly(reader);
IOUtils.closeQuietly(writer);
}
Resulting "temp" file ends up being corrupted: "extra bytes at beginning or
within zipfile " and "start of central directory not found; zipfile corrupt"
error messages when opening the file. If I try reading it using ZipInputStream,
I do see expected ZipEntry in it (in my case, the segments_# file, but I'm
seeing the same issue when streaming a .zip containing a simple ".txt" file as
well), but it has a size 0. Hoping my issue has something to do with the
contentType/encoding, but haven't been able to pinpoint the exact problem
(using "application/octet-stream" or "application/x-zip-compressed" doesn't
seem to change things). Please let me know if you have ideas.
Thanks!