package ch.rasc.musicsearch.web;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.lucene.document.Document;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import ch.rasc.musicsearch.service.IndexService;

import com.google.common.base.Splitter;
import com.google.common.io.ByteStreams;

@Controller
public class DownloadMusicZipController {

	@Autowired
	private Environment environement;

	@Autowired
	private IndexService indexService;

	@RequestMapping("/downloadMusicZip")
	public void export(@RequestParam(value = "sf", required = true) String selectedMusicIds,
			HttpServletResponse response) throws IOException {

		if (StringUtils.isNotBlank(selectedMusicIds)) {

			Path tempFile = Files.createTempFile("mp3", "zip");
			tempFile.toFile().deleteOnExit();

			try (OutputStream os = Files.newOutputStream(tempFile);
					BufferedOutputStream bos = new BufferedOutputStream(os);
					ZipOutputStream zip = new ZipOutputStream(bos)) {
				zip.setMethod(ZipOutputStream.DEFLATED);
				zip.setLevel(Deflater.NO_COMPRESSION);

				for (String id : Splitter.on(",").split(selectedMusicIds)) {
					Document doc = indexService.getIndexSearcher().doc(Integer.valueOf(id));

					if (doc != null) {

						Path musicFile = Paths.get(environement.getProperty("musicDir"), doc.get("directory"),
								doc.get("fileName"));
						try (InputStream is = Files.newInputStream(musicFile)) {
							ZipEntry entry = new ZipEntry(musicFile.getFileName().toString());
							entry.setTime(DateTime.now().getMillis());
							zip.putNextEntry(entry);
							zip.write(ByteStreams.toByteArray(is));
							zip.closeEntry();
						}
					}
				}
			}

			response.setContentLength((int) Files.size(tempFile));

			try (OutputStream out = response.getOutputStream()) {
				Files.copy(tempFile, out);
			}
		}
	}
}
@@ -31,44 +31,8 @@
 b;%0A%0A
-import java.io.BufferedInputStream;%0A
 impo
@@ -96,36 +96,8 @@
 on;%0A
-import java.io.InputStream;%0A
 impo
@@ -853,49 +853,8 @@
 ter;
-%0Aimport com.google.common.io.ByteStreams;
 %0A%0A@C
@@ -1295,45 +1295,8 @@
 p%22);
-%0A%09%09%09tempFile.toFile().deleteOnExit();
 %0A%0A%09%09
@@ -1854,72 +1854,8 @@
 ));%0A
-%09%09%09%09%09%09try (InputStream is = Files.newInputStream(musicFile)) %7B%0A%09
 %09%09%09%09
@@ -1929,17 +1929,16 @@
 ;%0A%09%09%09%09%09%09
-%09
 entry.se
@@ -1968,25 +1968,24 @@
 tMillis());%0A
-%09
 %09%09%09%09%09%09zip.pu
@@ -2005,25 +2005,24 @@
 try);%0A%09%09%09%09%09%09
-%09
 zip.write(By
@@ -2023,39 +2023,40 @@
 ite(
-ByteStreams.toByteArray(is
+Files.readAllBytes(musicFile
 ));%0A
-%09
 %09%09%09%09
@@ -2079,16 +2079,8 @@
 ();%0A
-%09%09%09%09%09%09%7D%0A
 %09%09%09%09
@@ -2246,16 +2246,47 @@
 );%0A%09%09%09%7D%0A
+%09%09%09%0A%09%09%09Files.delete(tempFile);%0A
 %09%09%7D%0A%09%7D%0A%7D

Use Java 7 method to write file into Zip Delete tempFile at the end