Вы можете создать zip-файл и добавить к нему, пока пользователь загружает его. Если вы используете сервлет, это просто:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ..... process request
// ..... then respond
response.setContentType("application/zip");
response.setStatus(HttpServletResponse.SC_OK);
// note : intentionally no content-length set, automatic chunked transfer if stream is larger than the internal buffer of the response
ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
byte[] buffer = new byte[1024 * 32];
try {
// case1: already have input stream, typically ByteArrayInputStream from a byte[] full of previoiusly prepared csv data
InputStream in = new BufferedInputStream(getMyFirstInputStream());
try {
zipOut.putNextEntry(new ZipEntry("FirstName"));
int length;
while((length = in.read(buffer)) != -1) {
zipOut.write(buffer, 0, length);
}
zipOut.closeEntry();
} finally {
in.close();
}
// case 2: write directly to output stream, i.e. you have your raw data but need to create csv representation
zipOut.putNextEntry(new ZipEntry("SecondName"));
// example setup, key is to use the below outputstream 'zipOut' write methods
Object mySerializer = new MySerializer(); // i.e. csv-writer
Object myData = getMyData(); // the data to be processed by the serializer in order to make a csv file
mySerizalier.setOutput(zipOut);
// write whatever you have to the zipOut
mySerializer.write(myData);
zipOut.closeEntry();
// repeat for the next file.. or make for-loop
}
} finally {
zipOut.close();
}
}
Нет причин хранить ваши данные в файлах, если у вас нет ограничений памяти. Файлы дают вам InputStream и OutputStream, оба из которых имеют эквиваленты в памяти.
Обратите внимание, что создание модуля записи в csv обычно означает выполнение чего-то вроде this , где смысл состоит в том, чтобы взять часть данных (список массивов или карту, что у вас есть) и превратить их в части байта [] , Добавьте части byte [] в OutputStream с помощью такого инструмента, как DataOutputStream (создайте свой собственный, если хотите) или OutputStreamWriter.