<commit_before>package org.openforis.collect.io;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.openforis.collect.utils.Files;

/**
 * 
 * @author <NAME>
 *
 */
public class NewBackupFileExtractor implements Closeable {

	public static final String RECORD_FILE_DIRECTORY_NAME = "upload";
	private static final String ZIP_SEPARATOR_PATTERN = "[\\\\|/]";

	//input variables
	private File file;
	
	private File tempUncompressedFolder;
	//temporary variables
	private transient ZipFile zipFile;

	public NewBackupFileExtractor(File file) throws ZipException, IOException {
		this.file = file;
	}

	public void init() throws IOException {
		tempUncompressedFolder = Files.createTempDirectory();
		zipFile = new ZipFile(file);
		Enumeration<? extends ZipEntry> entries = zipFile.entries();
		while (entries.hasMoreElements()) {
			ZipEntry zipEntry = entries.nextElement();
			if (! zipEntry.isDirectory()) {
				String entryName = zipEntry.getName();
				File folder = getOrCreateEntryFolder(entryName);
				String fileName = extractFileName(entryName);
				File newFile = new File(folder, fileName);
				newFile.createNewFile();
				InputStream is = zipFile.getInputStream(zipEntry);
				FileUtils.copyInputStreamToFile(is, newFile);
			}
		}
	}
	
	public File extractInfoFile() {
		return extract(SurveyBackupJob.INFO_FILE_NAME);
	}
	
	public SurveyBackupInfo extractInfo() {
		try {
			File infoFile = extractInfoFile();
			SurveyBackupInfo info = SurveyBackupInfo.parse(new FileInputStream(infoFile));
			return info;
		} catch (Exception e) {
			throw new RuntimeException("Error extracting info file from archive", e);
		}
	}
	
	public File extractIdmlFile() {
		return extract(SurveyBackupJob.SURVEY_XML_ENTRY_NAME);
	}
	
	public File extract(String entryName) {
		return extract(entryName, true);
	}
	
	public File extract(String entryName, boolean required) {
		File folder = getOrCreateEntryFolder(entryName);
		String fileName = extractFileName(entryName);
		File result = new File(folder, fileName);
		return result.exists() ? result: null;
	}

	public List<String> listFilesInFolder(String folderPath) {
		List<String> result = new ArrayList<String>();
		File folder = getOrCreateFolder(folderPath);
		File[] files = folder.listFiles();
		for (File file : files) {
			result.add(file.getName());
		}
		return result;
	}
	
	public List<String> listSpeciesEntryNames() {
		List<String> entries = listFilesInFolder(SurveyBackupJob.SPECIES_FOLDER);
		return entries;
	}
	
	public List<File> extractFilesInPath(String folder) throws IOException {<commit_msg>Sort files in data import<commit_after>package org.openforis.collect.io;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.openforis.collect.utils.Files;

/**
 * 
 * @author <NAME>
 *
 */
public class NewBackupFileExtractor implements Closeable {

	public static final String RECORD_FILE_DIRECTORY_NAME = "upload";
	private static final String ZIP_SEPARATOR_PATTERN = "[\\\\|/]";

	//input variables
	private File file;
	
	private File tempUncompressedFolder;
	//temporary variables
	private transient ZipFile zipFile;

	public NewBackupFileExtractor(File file) throws ZipException, IOException {
		this.file = file;
	}

	public void init() throws IOException {
		tempUncompressedFolder = Files.createTempDirectory();
		zipFile = new ZipFile(file);
		Enumeration<? extends ZipEntry> entries = zipFile.entries();
		while (entries.hasMoreElements()) {
			ZipEntry zipEntry = entries.nextElement();
			if (! zipEntry.isDirectory()) {
				String entryName = zipEntry.getName();
				File folder = getOrCreateEntryFolder(entryName);
				String fileName = extractFileName(entryName);
				File newFile = new File(folder, fileName);
				newFile.createNewFile();
				InputStream is = zipFile.getInputStream(zipEntry);
				FileUtils.copyInputStreamToFile(is, newFile);
			}
		}
	}
	
	public File extractInfoFile() {
		return extract(SurveyBackupJob.INFO_FILE_NAME);
	}
	
	public SurveyBackupInfo extractInfo() {
		try {
			File infoFile = extractInfoFile();
			SurveyBackupInfo info = SurveyBackupInfo.parse(new FileInputStream(infoFile));
			return info;
		} catch (Exception e) {
			throw new RuntimeException("Error extracting info file from archive", e);
		}
	}
	
	public File extractIdmlFile() {
		return extract(SurveyBackupJob.SURVEY_XML_ENTRY_NAME);
	}
	
	public File extract(String entryName) {
		return extract(entryName, true);
	}
	
	public File extract(String entryName, boolean required) {
		File folder = getOrCreateEntryFolder(entryName);
		String fileName = extractFileName(entryName);
		File result = new File(folder, fileName);
		return result.exists() ? result: null;
	}

	public List<String> listFilesInFolder(String folderPath) {
		List<String> result = new ArrayList<String>();
		File folder = getOrCreateFolder(folderPath);
		File[] files = folder.listFiles();
		for (File file : files) {
			result.add(file.getName());
		}
		Collections.sort(result);
		return result;
	}
	
	public List<String> listSpeciesEntryNames() {
		List<String> entries = listFilesInFolder(SurveyBackupJob.SPECIES_FOLDER);
		return entries;
	}
	
	public List<File> extractFilesInPath(String folder) throws IOException {
{'fraction_non_alphanumeric': 0.10493409935789118, 'fraction_numerical': 0.0, 'mean_word_length': 4.924924924924925, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '20133', 'n_tokens_mistral': 1932, 'n_tokens_neox': 1762, 'n_words': 427}