/**
 * ****************************************************************************
 * Copyright 2014 Virginia Polytechnic Institute and State University
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ****************************************************************************
 */
package edu.vt.vbi.patric.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class CreateZip {

	private static final Logger LOGGER = LoggerFactory.getLogger(CreateZip.class);

	private final String DOWNLOAD_ROOT = "/cid/brcdownloads/patric2/patric3/genomes/";

	public static void main(String args[]) throws IOException {

	}

	public byte[] ZipIt(List<String> genomeIdList, List<String> annotations, List<String> fileTypes) throws IOException {

		Set<String> files = new HashSet<>();

		for (String genomeId : genomeIdList) {

			File genomeDir = new File(DOWNLOAD_ROOT + genomeId);

			if (!genomeDir.exists()) {
				LOGGER.debug("Skipping Folder: {}", DOWNLOAD_ROOT + genomeId);
				continue;
			}
			else {
				for (String annotation : annotations) {
					for (String fileType : fileTypes) {
						File file = new File(DOWNLOAD_ROOT + genomeId + "/" + genomeId + (fileType.equals(".fna") ? "" : annotation) + fileType);

						if (!file.exists()) {
							System.err.println("Skipping File: " + file.getAbsolutePath());
						}
						else {
							files.add(file.getAbsolutePath());
						}
					}
				}
			}
		}

		try (
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				ZipOutputStream zos = new ZipOutputStream(baos)
		) {

			if (!files.isEmpty()) {

				int bytesRead;
				byte[] buffer = new byte[1024];

				for (String path : files) {
					File file = new File(path);
					BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
					ZipEntry entry = new ZipEntry(file.getName());
					zos.putNextEntry(entry);
					while ((bytesRead = bis.read(buffer)) != -1) {
						zos.write(buffer, 0, bytesRead);
					}
					bis.close();
					zos.closeEntry();
				}
				zos.close();
			}
			return baos.toByteArray();
		}
	}
}
@@ -1948,26 +1948,20 @@
 %09%09%09%09
-System.err.println
+LOGGER.error
 (%22Sk
@@ -1973,19 +1973,20 @@
 g File: 
-%22 +
+%7B%7D%22,
  file.ge
@@ -2258,64 +2258,8 @@
  %7B%0A%0A
-%09%09%09%09int bytesRead;%0A%09%09%09%09byte%5B%5D buffer = new byte%5B1024%5D;%0A%0A
 %09%09%09%09
@@ -2411,25 +2411,25 @@
 %09%09%09%09
-ZipEntry e
+zos.putNextE
 ntry
- = 
+(
 new 
@@ -2456,38 +2456,67 @@
 e())
+)
 ;%0A
+%0A
 %09%09%09%09%09
-zos.putNextEntry(entry)
+int bytesRead;%0A%09%09%09%09%09byte%5B%5D buffer = new byte%5B1024%5D
 ;%0A%09%09
@@ -2615,26 +2615,8 @@
 %09%09%7D%0A
-%09%09%09%09%09bis.close();%0A
 %09%09%09%09
@@ -2630,24 +2630,42 @@
 oseEntry();%0A
+%09%09%09%09%09bis.close();%0A
 %09%09%09%09%7D%0A%09%09%09%09zo

switch system log to Logger, cleanup