package com.qmth.jkserver;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import com.qmth.jkserver.constant.SystemConstant;
import org.apache.commons.io.IOUtils;

public class ZipTest {
	
	public static void main(String[] args) throws IOException {
		ZipOutputStream zipOutputStream = null;
		try {
			File dir = new File("F:\\yyb_test");
			File storeDir = new File(dir, "src");
			File zip = new File(dir,"测试-"+System.currentTimeMillis()+SystemConstant.ZIP_PREFIX);
			File file2 = new File(dir, "1.pdf");
			zipOutputStream = new ZipOutputStream(new FileOutputStream(zip));
			File zipFile = new File(dir,"c1f1a8a0-4af0-4757-afb0-ac16c9fd0a7a.zip");
			ZipFile pFile = new ZipFile(zipFile);
			if(zipFile.exists()){
				InputStream inputStream = new FileInputStream(zipFile);
				ZipInputStream zipInputStream = new ZipInputStream(inputStream, Charset.forName(SystemConstant.CHARSET_NAME));
				ZipEntry zipEntry = null;
				while((zipEntry = zipInputStream.getNextEntry()) != null) {
					if(!zipEntry.isDirectory() && zipEntry.getName().indexOf(File.separator) > 0){
						zipOutputStream.putNextEntry(zipEntry);
						InputStream inputStream2 = pFile.getInputStream(zipEntry);
						IOUtils.copy(inputStream2, zipOutputStream);
						zipOutputStream.closeEntry();
						IOUtils.closeQuietly(inputStream2);
					}
				}
				IOUtils.closeQuietly(inputStream);
				IOUtils.closeQuietly(zipInputStream);
			}
			zipOutputStream.putNextEntry(new ZipEntry("333"+File.separator+file2.getName()));
			InputStream iStream = new FileInputStream(file2);
			IOUtils.copy(iStream, zipOutputStream);
			IOUtils.closeQuietly(zipOutputStream);
			IOUtils.closeQuietly(iStream);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			zipOutputStream.close();
		}
	}

}
