Untitled

                Never    
Java
       
package it4kt.web.jsf.bean;

import it4kt.web.utils.Utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Enumeration;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.compress.utils.IOUtils;
import org.primefaces.event.FileUploadEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The Class TaskBean for tasks operations.
 */
public class TaskBean implements Serializable {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/** The Constant LOGGER. */
	private static final Logger LOGGER = LoggerFactory
			.getLogger(TaskBean.class);

	// please use some spring property for that, do not hardcode that
	private String basePath = "/home/mistvan/it4ktControlSystem/filesystem/temp";

	/**
	 * Handle file upload.
	 * 
	 * @param event
	 *            the event
	 */
	public void handleFileUpload(FileUploadEvent event) {
		LOGGER.debug("handleFileUpload()");
		InputStream inputStream = null;
		BufferedOutputStream buffOutStream = null;

		File uploadDir = new File(basePath);
		File fileToStore = new File(uploadDir,event.getFile().getFileName());
		ZipFile zipFile = null;

		try {
			inputStream = event.getFile().getInputstream();
			buffOutStream = new BufferedOutputStream(new FileOutputStream(fileToStore));
			IOUtils.copy(inputStream, buffOutStream);
			buffOutStream.flush();
		} catch (Exception e) {
			// log exception here
		}finally{
			
			// try to close all streams properly
			if (buffOutStream!=null) {
				try {
					buffOutStream.close();
				} catch (IOException e) {
					// log exception here
				}
			}
			if (inputStream!=null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					// log exception here
				}
			}
		}
		
		try {
			zipFile = new ZipFile(fileToStore);
		} catch (IOException e) {
			// throwni exception, alebo zaloguj a ukonci cez return;
		}

		// get entries
		final Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
		while (entries.hasMoreElements()) {
			// get entry
			final ZipArchiveEntry entry = entries.nextElement();

			// if directory just create
			if (entry.isDirectory()) {
				new File(uploadDir, entry.getName()).mkdirs();
				continue;
			}
			// if normal file create in on FS
			final String name = entry.getName();
			File fileToStoreLocal = new File(uploadDir, name);

			try {
				inputStream = zipFile.getInputStream(entry);
				buffOutStream = new BufferedOutputStream(new FileOutputStream(
						fileToStoreLocal));
				IOUtils.copy(inputStream, buffOutStream);
				buffOutStream.flush();
			} catch (Exception e) {
				// log exception here
			} finally {

				// try to close all streams properly
				if (buffOutStream != null) {
					try {
						buffOutStream.close();
					} catch (IOException e) {
						// log exception here
					}
				}
				if (inputStream != null) {
					try {
						inputStream.close();
					} catch (IOException e) {
						// log exception here
					}
				}
			}
		}

		// when all finished

		try {
			zipFile.close();
		} catch (IOException e) {
			// log exception here
		}

		FacesContext.getCurrentInstance().addMessage(
				null,
				new FacesMessage(FacesMessage.SEVERITY_INFO, Utils
						.getMessage("common.message.info.fileUploaded"), null));

	}
}

Raw Text