package org.gbif.crawler.dwca.util;

import org.gbif.dwca.io.Archive;
import org.gbif.dwca.io.ArchiveFactory;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import com.google.common.io.Files;
import org.apache.commons.io.FileUtils;

public class DwcaTestUtil {

  /**
   * Copies a zip archive from the test resources into a random uuid folder and returns the opened archive.
   */
  public static Archive openArchive(String archiveResourcePath) throws IOException {
    UUID uuid = UUID.randomUUID();

    File zipFile = new File(DwcaTestUtil.class.getResource(archiveResourcePath).getFile());
    File tmpFile = new File(zipFile.getParentFile(), uuid.toString() + ".dwca");
    Files.copy(zipFile, tmpFile);

    File dwcaDir = new File(tmpFile.getParent(), uuid.toString());
    return ArchiveFactory.openArchive(tmpFile, dwcaDir);
  }

  public static void cleanupArchive(Archive archive) {
    File zip = archive.getLocation();
    FileUtils.deleteQuietly(zip);
  }

}

@@ -251,16 +251,60 @@
 leUtils;
+%0Aimport org.apache.commons.io.FilenameUtils;
 %0A%0Apublic
@@ -358,16 +358,33 @@
 archive 
+or a single file 
 from the
@@ -593,19 +593,19 @@
    File 
-zip
+src
 File = n
@@ -704,19 +704,19 @@
 ew File(
-zip
+src
 File.get
@@ -772,19 +772,19 @@
 es.copy(
-zip
+src
 File, tm
@@ -867,60 +867,198 @@
     
-return ArchiveFactory.openArchive(tmpFile, dwcaDir);
+if(%22zip%22.equalsIgnoreCase(FilenameUtils.getExtension(srcFile.getName()))) %7B%0A      return ArchiveFactory.openArchive(tmpFile, dwcaDir);%0A    %7D%0A%0A    return ArchiveFactory.openArchive(tmpFile);%0A
 %0A  %7D

allow DwcaTestUtil to load a EML file