package kr.co.udapsoft.ebid.buyer.common.file;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kr.co.hsnc.common.config.WAFConfig;
import kr.co.hsnc.common.util.StringUtil;
import kr.co.hsnc.common.util.Translator;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;

import net.sf.jazzlib.ZipEntry;
import net.sf.jazzlib.ZipOutputStream;



/**
 * 
 *<ul>
 * <li>프  로  그  램 : ZipUtil.java</li>
 * <li>프로그램설명 : 압축 유틸리티</li>
 * <li>관련프로그램 : </li>
 * <li>관련  테이블 : </li>
 * <li>작  성  일  자 : </li>
 * <li>수  정  내  역 : </li>
 * <li>참  고  사  항 : </li>
 * </ul>
 * @author 
 */
public class ZipUtil {
 
	private static String getShortFileName( String fullName )
	{
		int p = fullName.lastIndexOf('/');
		if(p<0)
			p = fullName.lastIndexOf('\\');
		if(p<0)
			return fullName;
		return fullName.substring(p+1);
	}
 
	public static int createZipFile(List files, String outFileName) 
	{
		final int COMPRESSION_LEVEL = 9 ; 
		int SIZE = 65536; 
		byte[] buf = new byte[SIZE];
		int len;
		
		try {
			ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFileName)));
			
			for(int i = 0 ; i < files.size() ; i++ )
			{
				String fullFileName = (String)files.get(i);
				String shortFileName = getShortFileName(fullFileName);
				FileInputStream fs = new FileInputStream(fullFileName); 
				BufferedInputStream in = new BufferedInputStream(fs , SIZE); 
				
				out.putNextEntry(new ZipEntry(shortFileName));
				out.setLevel(COMPRESSION_LEVEL);

	            len = 0; 
	            while ((len = in.read(buf, 0, SIZE)) > 0) {
	                out.write(buf, 0, len);
	            }
	            out.closeEntry();
	            in.close();
			}
	            out.close();
        }catch(Exception e) {

            e.printStackTrace() ; 
        }
		return 0; 
	}
	
	// 한글깨짐으로 아파치 라이브러리(commons-compress) 사용
	public static int createZipFile(Map<String, String> files, String outFileName) 
	{
		final int COMPRESSION_LEVEL = 8 ; 
		int SIZE = 4096; 
		byte[] buf = new byte[SIZE];
		int len;
		
		try {
			ZipArchiveOutputStream out = new ZipArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(outFileName)));
			
			for( String key : files.keySet() ){
				String fullFileName = key;
				FileInputStream fs = new FileInputStream(fullFileName); 
				BufferedInputStream in = new BufferedInputStream(fs , SIZE); 
				
				out.putArchiveEntry(new ZipArchiveEntry(files.get(key)));
				out.setLevel(COMPRESSION_LEVEL);

	            len = 0; 
	            while ((len = in.read(buf, 0, SIZE)) > 0) {
	                out.write(buf, 0, len);
	            }
	            out.closeArchiveEntry();
	            in.close();
	            fs.close();
			}
			
	        out.close();
        }catch(Exception e) {

            e.printStackTrace() ; 
        }
		return 0; 
	}
	
	public static List unzipFile(String zipFile,String targetDir) throws Exception{
		ZipUtils2 zu2=new ZipUtils2();
		List fileList=new ArrayList();
		zu2.unZipFile(zipFile, targetDir,fileList);
		return fileList;
	}
	
	public static void main(String[] args)throws Exception{
		//List fileList = new ArrayList();
		//fileList.add("C:/project/EBID/files/bid/outside/INQ-MR PP4-EL-317_001/39819/20200221141922026_diBWtGbMP6..zip");
		//fileList.add("C:/project/EBID/files/cont/purchase/35340/35340_1_1.pdf");
		//File testFile = new File("C:/project/EBID/files/bid/outside/INQ-MR PP4-EL-317_001/39819/zip");
		
		//Map<String, String> fileMap = new HashMap<String, String>();
		//fileMap.put("C:/project/EBID/files/bid/outside/INQ-MR PP4-EL-317_001/39819/20200221141922026_diBWtGbMP6..zip", "Priced.zip");
		
		String outDir = "/bid/outside/INQ-MR PP4-EL-317_001/39819" + "/ZIP";
		String filePath = StringUtil.replace(outDir + "/" + Translator.translate("8859_1", "UTF-8", StringUtil.replace("Control System(DCS/SIS)(MR-D17044-ESP030-001-1 )_(주)ABB코리아 ","/",".")) , "/", File.separator);
		System.out.println(filePath);
		//if(!testFile.exists()){
		//	testFile.mkdirs();
		//}		
		//createZipFile(fileList, testFile.getAbsolutePath() + "/테스트.zip");
		//createZipFile(fileMap, testFile.getAbsolutePath() + "/테스트.zip");
	}
}
