A side note: try-with-resources statements were introduced in Java 7.
The resources to be disposed of in this case are the FileOutputStream, the ZipOutputStream and the FileInputStream. Formally speaking, they can be used in try-with-resources because they implement AutoCloseable. So you can write the code as follows:
Java 7+
public void archivingTheFile(String zipFile){ byte[] buffer = new byte[1024]; try(FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zos = new ZipOutputStream(fos)) { for(String file : this.fileList){ ZipEntry ze= new ZipEntry(file); zos.putNextEntry(ze); try(FileInputStream in = new FileInputStream(SOURCE_FOLDER + File.separator + file)) { int len; while ((len = in.read(buffer)) > 0) { zos.write(buffer, 0, len); } } } zos.closeEntry(); }catch(IOException ex){ LOGGER.error("Exception occurred while zipping file",ex); } }
Notice that you don't need to call FileOutputStream.close() or FileInputStream.close().
Why is Sonar telling you to use these statements?
It's because they are the best way to deal with objects that represent resources such as IO streams while ensuring that they are closed at the end. Otherwise, resources may leak in your system. With Java 6 code, the alternative is to use try-catch-finally:
Java 6
FileOutputStream fos = null; ZipOutputStream zos = null; try { fos = new FileOutputStream(zipFile); zos = new ZipOutputStream(fos) ... } catch(IOException ex){ LOGGER.error("Exception occurred while zipping file",ex); } finally { if(fos != null) { fos.close(); } if(zos != null) { zos.close(); } }
You can read about the try-with-resources statement from the Java tutorials: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html.