title: Spring REST - create ZIP file and send it to the client
body: <p>I want to create a ZIP file that contains my archived files that I received from the backend, and then send this file to a user. For 2 days I have been looking for the answer and can't find proper solution, maybe you can help me :)</p>
  <p><strong>For now, the code is like this</strong> (I know I shouldn't do it all in the Spring controller, but don't care about that, it is just for testing purposes, to find the way to make it works):</p>
  <pre><code>    @RequestMapping(value = &quot;/zip&quot;)
      public byte[] zipFiles(HttpServletResponse response) throws IOException {
          // Setting HTTP headers
          response.setContentType(&quot;application/zip&quot;);
          response.setStatus(HttpServletResponse.SC_OK);
          response.addHeader(&quot;Content-Disposition&quot;, &quot;attachment; filename=\&quot;test.zip\&quot;&quot;);
  
          // Creating byteArray stream, make it bufferable and passing this buffer to ZipOutputStream
          ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
          BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
          ZipOutputStream zipOutputStream = new ZipOutputStream(bufferedOutputStream);
  
          // Simple file list, just for tests
          ArrayList&lt;File&gt; files = new ArrayList&lt;&gt;(2);
          files.add(new File(&quot;README.md&quot;));
  
          // Packing files
          for (File file : files) {
              // New zip entry and copying InputStream with file to ZipOutputStream, after all closing streams
              zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
              FileInputStream fileInputStream = new FileInputStream(file);
  
              IOUtils.copy(fileInputStream, zipOutputStream);
  
              fileInputStream.close();
              zipOutputStream.closeEntry();
          }
  
          if (zipOutputStream != null) {
              zipOutputStream.finish();
              zipOutputStream.flush();
              IOUtils.closeQuietly(zipOutputStream);
          }
          IOUtils.closeQuietly(bufferedOutputStream);
          IOUtils.closeQuietly(byteArrayOutputStream);
  
          return byteArrayOutputStream.toByteArray();
      }
  </code></pre>
  <p>But the problem is, that using the code, when I enter the URL <code>localhost:8080/zip</code>, I get a file <code>test.zip.html</code> instead of <code>.zip</code> file.</p>
  <p>When I remove <code>.html</code> extension and leave just <code>test.zip</code> it opens correctly. So my questions are:</p>
  <ul>
  <li>How to avoid returning this <code>.html</code> extension?</li>
  <li>Why is it added?</li>
  </ul>
  <hr />
  <p>I have no idea what else can I do. I was also trying replace <code>ByteArrayOuputStream</code> with something like:</p>
  <pre><code>OutputStream outputStream = response.getOutputStream();
  </code></pre>
  <p>and set the method to be <strong>void</strong> so it returns nothing, but It created <code>.zip</code> file which was damaged?</p>
  <p>On my MacBook after unpacking the <code>test.zip</code> I was getting <code>test.zip.cpgz</code> which was again giving me <code>test.zip</code> file and so on.</p>
  <p>On Windows the .zip file was damaged as I said and couldn't even open it.</p>
  <p>I also suppose, that removing <code>.html</code> extension automatically will be the best option, but how?</p>
  <p>Hope it is no as hard as It seems to be :)<br />
  Thanks</p>