package org.ihtsdo.snomed.rf2torf1conversion;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.apache.commons.io.IOUtils;

public class GlobalUtils {

	public static boolean verbose;

	public static void print(String msg) {
		System.out.println(msg);
	}

	public static void exit() {
		System.exit(-1);
	}

	public static void debug(String msg) {
		if (verbose) {
			System.out.println(msg);
		}
	}

	public static void unzipFlat(File archive, File targetDir, String matchStr) throws RF1ConversionException {

		if (!targetDir.exists() || !targetDir.isDirectory()) {
			throw new RF1ConversionException(targetDir + " is not a viable directory in which to extract archive");
		}
		try {
			ZipInputStream zis = new ZipInputStream(new FileInputStream(archive));
			ZipEntry ze = zis.getNextEntry();
			try {
				while (ze != null) {
					if (!ze.isDirectory()) {
						Path p = Paths.get(ze.getName());
						String extractedFileName = p.getFileName().toString();
						if (matchStr == null || extractedFileName.contains(matchStr)) {
							debug("Extracting " + extractedFileName);
							File extractedFile = new File(targetDir, extractedFileName);
							OutputStream out = new FileOutputStream(extractedFile);
							IOUtils.copy(zis, out);
							IOUtils.closeQuietly(out);
						}
					}
					ze = zis.getNextEntry();
				}
			} finally {
				zis.closeEntry();
				zis.close();
			}
		} catch (IOException e) {
			throw new RF1ConversionException("Failed to expand archive " + archive.getName(), e);
		}
	}

	public static String findDateInString(String str, boolean optional) throws RF1ConversionException {
		Matcher dateMatcher = Pattern.compile("(\\d{8})").matcher(str);
		if (dateMatcher.find()) {
			return dateMatcher.group();
		} else {
			if (optional) {
				print("Did not find a date in: " + str);
			} else {
				throw new RF1ConversionException("Unable to determine date from " + str);
			}
		}
		return null;
	}
}

@@ -407,16 +407,54 @@
 tStream;
+%0Aimport java.util.zip.ZipOutputStream;
 %0A%0Aimport
@@ -2292,11 +2292,1223 @@
 null;%0A%09%7D
+%0A%0A%09public static void createArchive(File exportLocation) throws RF1ConversionException %7B%0A%09%09try %7B%0A%09%09%09// The zip filename will be the name of the first thing in the zip location%0A%09%09%09// ie in this case the directory SnomedCT_RF1Release_INT_20150731%0A%09%09%09String zipFileName = exportLocation.listFiles()%5B0%5D.getName();%0A%09%09%09ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));%0A%09%09%09print(%22Creating archive : %22 + zipFileName);%0A%09%09%09addDir(exportLocation, out);%0A%09%09%09out.close();%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09throw new RF1ConversionException(%22Failed to create RF1 Archive from %22 + exportLocation, e);%0A%09%09%7D%0A%09%7D%0A%0A%09public static void addDir(File dirObj, ZipOutputStream out) throws IOException %7B%0A%09%09File%5B%5D files = dirObj.listFiles();%0A%09%09byte%5B%5D tmpBuf = new byte%5B1024%5D;%0A%0A%09%09for (int i = 0; i %3C files.length; i++) %7B%0A%09%09%09if (files%5Bi%5D.isDirectory()) %7B%0A%09%09%09%09addDir(files%5Bi%5D, out);%0A%09%09%09%09continue;%0A%09%09%09%7D%0A%09%09%09FileInputStream in = new FileInputStream(files%5Bi%5D.getAbsolutePath());%0A%09%09%09debug(%22 Adding: %22 + files%5Bi%5D.getAbsolutePath());%0A%09%09%09out.putNextEntry(new ZipEntry(files%5Bi%5D.getAbsolutePath()));%0A%09%09%09int len;%0A%09%09%09while ((len = in.read(tmpBuf)) %3E 0) %7B%0A%09%09%09%09out.write(tmpBuf, 0, len);%0A%09%09%09%7D%0A%09%09%09out.closeEntry();%0A%09%09%09in.close();%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

Add support for creating output RF1 archive