/*
 * Copyright 2017-2018 Nikita Shakarun
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ru.playsoftware.j2meloader.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class ZipUtils {

	public static void zip(File sourceFolder, File zipFile) throws IOException {
		FileOutputStream dest = new FileOutputStream(zipFile);
		ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
		zipSubFolder(out, sourceFolder, sourceFolder.getPath().length() + 1);
		out.close();
	}

	private static void zipSubFolder(ZipOutputStream out, File folder,
									 int basePathLength) throws IOException {
		File[] fileList = folder.listFiles();
		BufferedInputStream origin;
		for (File file : fileList) {
			if (file.isDirectory()) {
				zipSubFolder(out, file, basePathLength);
			} else {
				String unmodifiedFilePath = file.getPath();
				String relativePath = unmodifiedFilePath.substring(basePathLength);
				FileInputStream fi = new FileInputStream(unmodifiedFilePath);
				origin = new BufferedInputStream(fi);
				ZipEntry entry = new ZipEntry(relativePath);
				out.putNextEntry(entry);
				IOUtils.copy(origin, out);
				origin.close();
			}
		}
	}

	public static void unzip(File zipFile, File extractFolder) throws IOException {
		ZipFile zip = new ZipFile(zipFile);
		extractFolder.mkdir();
		Enumeration zipFileEntries = zip.entries();
		while (zipFileEntries.hasMoreElements()) {
			ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
			String currentEntry = entry.getName();
			File destFile = new File(extractFolder, currentEntry);
			File destinationParent = destFile.getParentFile();
			destinationParent.mkdirs();
			if (!entry.isDirectory() && !destFile.exists() && !entry.getName().endsWith(".class")) {
				BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
				// write the current file to disk
				FileOutputStream fos = new FileOutputStream(destFile);
				BufferedOutputStream dest = new BufferedOutputStream(fos);
				IOUtils.copy(is, dest);
				dest.flush();
				dest.close();
				is.close();
			}
		}
	}
}

@@ -738,40 +738,8 @@
 le;%0A
-import java.io.FileInputStream;%0A
 impo
@@ -890,1049 +890,32 @@
 le;%0A
-import java.util.zip.ZipOutputStream;%0A%0Apublic class ZipUtils %7B%0A%0A%09public static void zip(File sourceFolder, File zipFile) throws IOException %7B%0A%09%09FileOutputStream dest = new FileOutputStream(zipFile);%0A%09%09ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));%0A%09%09zipSubFolder(out, sourceFolder, sourceFolder.getPath().length() + 1);%0A%09%09out.close();%0A%09%7D%0A%0A%09private static void zipSubFolder(ZipOutputStream out, File folder,%0A%09%09%09%09%09%09%09%09%09 int basePathLength) throws IOException %7B%0A%09%09File%5B%5D fileList = folder.listFiles();%0A%09%09BufferedInputStream origin;%0A%09%09for (File file : fileList) %7B%0A%09%09%09if (file.isDirectory()) %7B%0A%09%09%09%09zipSubFolder(out, file, basePathLength);%0A%09%09%09%7D else %7B%0A%09%09%09%09String unmodifiedFilePath = file.getPath();%0A%09%09%09%09String relativePath = unmodifiedFilePath.substring(basePathLength);%0A%09%09%09%09FileInputStream fi = new FileInputStream(unmodifiedFilePath);%0A%09%09%09%09origin = new BufferedInputStream(fi);%0A%09%09%09%09ZipEntry entry = new ZipEntry(relativePath);%0A%09%09%09%09out.putNextEntry(entry);%0A%09%09%09%09IOUtils.copy(origin, out);%0A%09%09%09%09origin.close();%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D
+%0Apublic class ZipUtils %7B
 %0A%0A%09p

Clean up ZipUtils