All Downloads are FREE. Search and download functionalities are using the official Maven repository.

prerna.util.ZipUtils Maven / Gradle / Ivy

The newest version!
package prerna.util;

import java.io.BufferedOutputStream;
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.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.google.gson.GsonBuilder;

import prerna.util.gson.GsonUtility;

public final class ZipUtils {

	private static final Logger classLogger = LogManager.getLogger(ZipUtils.class);

	// buffer for read and write data to file
	private static byte[] buffer = new byte[2048];

	// always need to use this for zipping up and unzipping
	// traversing will break if the file separator is a "\"
	// which is generated on windows
	public static final String FILE_SEPARATOR = "/";

	private ZipUtils() {

	}

	/**
	 * Zip files within a dir
	 * 
	 * @param folderPath
	 * @param zipFilePath
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static ZipOutputStream zipFolder(String folderPath, String zipFilePath)
			throws FileNotFoundException, IOException {
		return zipFolder(folderPath, zipFilePath, null, null);
	}

	/**
	 * Zip files within a dir
	 * 
	 * @param folderPath
	 * @param zipFilePath
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static ZipOutputStream zipFolder(String folderPath, String zipFilePath, List ignoreDirs,
			List ignoreFiles) throws FileNotFoundException {
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(zipFilePath);
		} catch (Exception e) {
			classLogger.error("Could not find file for zip file path: {}", zipFilePath);
			classLogger.error(Constants.STACKTRACE, e);
			throw new IllegalArgumentException("Could not find file. See logs for details.");
		}
		ZipOutputStream zos = new ZipOutputStream(fos);
		File dir = new File(folderPath);
		try {
			classLogger.info(
					"Adding to zip with details.\n folderPath: {}\n zipFilePath: {}\n ignoredDirs: {}\n ignoredFiles: {}",
					folderPath, zipFilePath, ignoreDirs, ignoreFiles);
			addAllToZip(dir, zos, null, ignoreDirs, ignoreFiles);
		} catch (Exception e) {
			classLogger.error(Constants.STACKTRACE, e);
			throw new IllegalArgumentException("Could not add folder to zip. See logs for details.");
		}
		return zos;
	}

	/**
	 * Add file to ZipOutputStream
	 * 
	 * @param file
	 * @param zos
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static void addToZipFile(File file, ZipOutputStream zos) throws FileNotFoundException, IOException {
		ZipEntry zipEntry = new ZipEntry(file.getName());
		zos.putNextEntry(zipEntry);

		FileInputStream fis = null;
		try {
			int length;
			fis = new FileInputStream(file);
			while ((length = fis.read(buffer)) >= 0) {
				zos.write(buffer, 0, length);
			}
		} finally {
			if (fis != null) {
				fis.close();
			}
		}
		zos.closeEntry();
	}

	/**
	 * 
	 * @param logger
	 * @param zos
	 * @param prefixForZip
	 * @param filePathToWrite
	 * @param objToWrite
	 * @throws IOException
	 */
	public static void zipObjectToFile(ZipOutputStream zos, String prefixForZip, String filePathToWrite,
			Object objToWrite) throws IOException {
		File newFile = new File(filePathToWrite);
		GsonUtility.writeObjectToJsonFile(newFile, new GsonBuilder().setPrettyPrinting().create(), objToWrite);
		ZipUtils.addToZipFile(newFile, zos, prefixForZip);
	}

	/**
	 * Add file to ZipOutputStream
	 * 
	 * @param file
	 * @param zos
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static void addToZipFile(File file, ZipOutputStream zos, String prefix)
			throws FileNotFoundException, IOException {
		ZipEntry zipEntry = null;
		if (prefix == null || prefix.isEmpty()) {
			zipEntry = new ZipEntry(file.getName());
		} else {
			zipEntry = new ZipEntry(prefix + FILE_SEPARATOR + file.getName());
		}
		zos.putNextEntry(zipEntry);

		FileInputStream fis = null;
		try {
			int length;
			fis = new FileInputStream(file);
			while ((length = fis.read(buffer)) >= 0) {
				zos.write(buffer, 0, length);
			}
		} finally {
			if (fis != null) {
				fis.close();
			}
		}
		zos.closeEntry();
	}

	/**
	 * 
	 * @param file
	 * @param zos
	 * @param prefix
	 * @param ignoreFiles
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	private static void addAllToZip(File file, ZipOutputStream zos, String prefix, List ignoreDirs,
			List ignoreFiles) throws FileNotFoundException, IOException {
		if (file.isDirectory()) {
			String subPrefix = file.getName();
			if (prefix != null) {
				subPrefix = prefix + FILE_SEPARATOR + file.getName();
			}
			// make sure its not in the ignore list of folders
			if (ignoreDirs == null || !ignoreDirs.contains(subPrefix)) {
				File[] files = file.listFiles();
				for (File subF : files) {
					addAllToZip(subF, zos, subPrefix, ignoreDirs, ignoreFiles);
				}
			}
		} else {
			String fileName = file.getName();
			if (prefix != null) {
				fileName = prefix + FILE_SEPARATOR + file.getName();
			}
			// make sure its not in the ignore list if we have one
			if (ignoreFiles == null || !ignoreFiles.contains(fileName)) {
				ZipEntry zipEntry = new ZipEntry(fileName);
				zos.putNextEntry(zipEntry);
				FileInputStream fis = null;
				try {
					int length;
					fis = new FileInputStream(file);
					while ((length = fis.read(buffer)) >= 0) {
						zos.write(buffer, 0, length);
					}
				} finally {
					if (fis != null) {
						fis.close();
					}
				}
				zos.closeEntry();
			}
		}
	}

	/**
	 * Unzip files to a folder and track files that have been added
	 * 
	 * @param zipFilePath
	 * @param destDirectory
	 * @return Map of list of files depending on if it is a DIR or FILE
	 * @throws IOException
	 */
	public static Map> unzip(String zipFilePath, String destination) throws IOException {
		// grab list of files that are being unzipped
		Map> files = listFilesInZip(Paths.get(zipFilePath));
		// unzip files
		ZipFile zipIn = null;
		try {
			zipIn = new ZipFile(Utility.normalizePath(zipFilePath));
			Enumeration entries = zipIn.entries();
			while (entries.hasMoreElements()) {
				ZipEntry entry = entries.nextElement();
				String filePath = destination + FILE_SEPARATOR + Utility.normalizePath(entry.getName());
				if (entry.isDirectory()) {
					File file = new File(filePath);
					file.mkdirs();
				} else {
					File parent = new File(filePath).getParentFile();
					if (!parent.exists()) {
						parent.mkdirs();
					}
					InputStream is = zipIn.getInputStream(entry);
					extractFile(is, filePath);
					is.close();
				}
			}
		} finally {
			if (zipIn != null) {
				zipIn.close();
			}
		}

		return files;
	}

	/**
	 * Copy file to path
	 * 
	 * @param zipIn
	 * @param filePath
	 * @throws IOException
	 */
	private static void extractFile(InputStream zipIn, String filePath) throws IOException {
		BufferedOutputStream bos = null;
		try {
			bos = new BufferedOutputStream(new FileOutputStream(Utility.normalizePath(filePath)));
			byte[] bytesIn = buffer;
			int read = 0;
			while ((read = zipIn.read(bytesIn)) != -1) {
				bos.write(bytesIn, 0, read);
			}
		} finally {
			try {
				if (bos != null) {
					bos.close();
				}
			} catch (IOException e) {
				classLogger.error(Constants.STACKTRACE, e);
			}
		}
	}

	/**
	 * https://stackoverflow.com/questions/15667125/read-content-from-files-which-are-inside-zip-file
	 * NOTE ::: Cleaning up paths to remove initial / to push files to git
	 * 
	 * @param fromZip
	 * @throws IOException
	 */
	public static Map> listFilesInZip(Path fromZip) throws IOException {
		FileSystem zipFs = null;
		Map> paths = new HashMap<>();
		Vector dirs = new Vector<>();
		Vector files = new Vector<>();
		try {
			zipFs = FileSystems.newFileSystem(fromZip, null);
			for (Path root : zipFs.getRootDirectories()) {
				Files.walkFileTree(root, new SimpleFileVisitor() {
					@Override
					public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
						// clean file path for git
						String filePath = file.toString();
						if (file.startsWith("/")) {
							filePath = filePath.replaceFirst("/", "");
							if (!filePath.equals(""))
								files.add(filePath);
						}
						return FileVisitResult.CONTINUE;
					}

					@Override
					public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
						// clean file path for git
						String pathDir = dir.toString().replaceFirst("/", "");
						if (!pathDir.equals("")) {
							dirs.add(pathDir);
						}

						return super.preVisitDirectory(dir, attrs);
					}
				});
			}
		} finally {
			try {
				if (zipFs != null) {
					zipFs.close();
				}
			} catch (IOException e) {
				classLogger.error(Constants.STACKTRACE, e);
			}
		}
		paths.put("DIR", dirs);
		paths.put("FILE", files);
		return paths;
	}

	/**
	 * 
	 * @param sourceFile
	 * @param gzipFile
	 * @throws IOException
	 */
	public static void compressGzipFile(String sourceFile, String gzipFile) throws IOException {
		try (FileOutputStream fos = new FileOutputStream(gzipFile);
				GZIPOutputStream gzipOS = new GZIPOutputStream(fos);
				FileInputStream fis = new FileInputStream(sourceFile)) {
			byte[] buffer = new byte[1024];
			int len;
			while ((len = fis.read(buffer)) > 0) {
				gzipOS.write(buffer, 0, len);
			}
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy