//
import java.util.*;
import java.io.*;
import java.util.zip.*;

public class FileFunctions 
{

 public static void  main(String arg[]) throws Exception 
 {
 
 }

/////////////////////////////////////////////////////////////////////////////	
/////////////////////////  FILE  READER        //////////////////////////////
/////////////////////////////////////////////////////////////////////////////
	private String readFile(String pathname) throws IOException {

		File file = new File(pathname);
		StringBuilder fileContents = new StringBuilder((int)file.length());
		Scanner scanner = new Scanner(file);
		String lineSeparator = System.getProperty("line.separator");

		try {
			while(scanner.hasNextLine()) {        
				fileContents.append(scanner.nextLine() + lineSeparator);
			}
			return fileContents.toString();
		} finally {
			scanner.close();
		}
	}
	
////////////////////////////////////////////////////////////////////////////////////////
///////////////////   Functie de citire a valorilor dintr-un fisier   //////////////////
////////////////////////////////////////////////////////////////////////////////////////	
		public  Properties readFileConfig(String pathname) throws IOException {
		
		String commentDelimiter = ";";
		String contentDelimiter = "=";
		Properties prop = new java.util.Properties();
		File file = new File(pathname);
		StringBuilder fileContents = new StringBuilder((int)file.length());
		Scanner scanner = new Scanner(file);
		String lineSeparator = System.getProperty("line.separator");
		
		try {
			while(scanner.hasNextLine()) {    
				String[] content = scanner.nextLine().split(commentDelimiter);
				String[] p = content[0].split(contentDelimiter);
				if(p.length == 2){
				prop.setProperty(p[0].trim(), p[1].trim());
				}
				
//				fileContents.append(scanner.nextLine() + lineSeparator);
			}
			// return fileContents.toString();
			return prop;
		} finally {
			scanner.close();
		}
	}

////////////////////////////////////////////////////////////////////////
//////////   ZIP COMPRESS FUNCTION                       ///////////////
////////////////////////////////////////////////////////////////////////

	public boolean fzip(String[] filenames, String outFilename){
	
	// These are the files to include in the ZIP file
//	String[] filenames = new String[]{"filename1", "filename2"};

	// Create a buffer for reading the files
	byte[] buf = new byte[1024];

	try {
		// Create the ZIP file
//		String outFilename = "outfile.zip";
		ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));

		// Compress the files
		for (int i=0; i<filenames.length; i++) {
			FileInputStream in = new FileInputStream(filenames[i]);

			// Add ZIP entry to output stream.
			out.putNextEntry(new ZipEntry(filenames[i]));

			// Transfer bytes from the file to the ZIP file
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}

			// Complete the entry
			out.closeEntry();
			in.close();
		}

		// Complete the ZIP file
		out.close();
		return true;
	} catch (IOException e) {
		return false;
	}

	
	
	}

////////////////////////////////////////////////////////////////////////
//////////   ZIP COMPRESS FUNCTION  cu optiune intrari   ///////////////
////////////////////////////////////////////////////////////////////////

	public boolean fzip(String[] filenames, String[] entries, String outFilename){
	
	// These are the files to include in the ZIP file
//	String[] filenames = new String[]{"filename1", "filename2"};

	// Create a buffer for reading the files
	byte[] buf = new byte[1024];

	try {
		// Create the ZIP file
//		String outFilename = "outfile.zip";
		ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));

		// Compress the files
		for (int i=0; i<filenames.length; i++) {
			FileInputStream in = new FileInputStream(filenames[i]);

			// Add ZIP entry to output stream.
			out.putNextEntry(new ZipEntry(entries[i]));

			// Transfer bytes from the file to the ZIP file
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}

			// Complete the entry
			out.closeEntry();
			in.close();
		}

		// Complete the ZIP file
		out.close();
		return true;
	} catch (IOException e) {
		return false;
	}

	
	
	}




	
	static String replace(String str, String pattern, String replace) {
		int s = 0;
		int e = 0;
		StringBuffer result = new StringBuffer();

		while ((e = str.indexOf(pattern, s)) >= 0) {
			result.append(str.substring(s, e));
			result.append(replace);
			s = e+pattern.length();
		}
		result.append(str.substring(s));
		return result.toString();
	}




	
}