You can create a zip file and add it while the user uploads it. If you are using a servlet, this is rather complicated:
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, ie 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(); // ie 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(); } }
There is no reason to store your data in files unless you have memory limits. The files give you InputStream and OutputStream, both of which have equivalents in memory.
Note that creating a csv record usually means doing something like this , where you need to take a piece of data (a list of arrays or a map, whatever you do) and turn it into bytes []. Add bytes [] to OutputStream using a tool such as DataOutputStream (create your own if you want) or OutputStreamWriter.
source share