Java tutorial
/* * PDFGalWeb * Copyright (c) 2014, Alejandro Pernas Pan, All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3.0 of the License, or (at your option) any later version. * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. */ package org.pdfgal.pdfgalweb.utils.impl; import java.io.BufferedInputStream; import java.io.DataInputStream; 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.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.pdfgal.pdfgalweb.utils.FileUtils; import org.pdfgal.pdfgalweb.utils.ZipUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class ZipUtilsImpl implements ZipUtils { @Autowired FileUtils fileUtils; @Override public String zipFiles(final List<String> urisList, final String fileName) throws FileNotFoundException, IOException { String generatedFileName = null; if (CollectionUtils.isNotEmpty(urisList) && StringUtils.isNotEmpty(fileName)) { // Autogeneration of file name generatedFileName = this.fileUtils.getAutogeneratedName(fileName + ".zip"); // ZIP file is created. final FileOutputStream fos = new FileOutputStream(generatedFileName); final ZipOutputStream zos = new ZipOutputStream(fos); // Files are added to ZIP. for (final String uri : urisList) { if (StringUtils.isNotEmpty(uri)) { this.addToZipFile(uri, zos, fileName); } } zos.close(); fos.close(); } return generatedFileName; } @Override public List<String> saveFilesFromZip(final InputStream inputStream) throws IOException { final List<String> result = new ArrayList<String>(); if (inputStream != null) { final ZipInputStream zis = new ZipInputStream(inputStream); ZipEntry entry = zis.getNextEntry(); while (entry != null) { final byte[] buffer = new byte[1024]; final String fileName = this.fileUtils.getAutogeneratedName(entry.getName()); final File file = new File(fileName); final FileOutputStream fos = new FileOutputStream(file); int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); result.add(fileName); entry = zis.getNextEntry(); } zis.closeEntry(); zis.close(); } return result; } @Override public boolean isZip(final InputStream inputStream) { boolean result = false; try { final DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(inputStream)); final int test = dataInputStream.readInt(); dataInputStream.close(); result = (test == 0x504b0304); } catch (final IOException e) { result = false; } return result; } /** * Adds a new file to the {@link ZipOutputStream}. * * @param fileName * @param zos * @param originalFileName * @throws FileNotFoundException * @throws IOException */ private void addToZipFile(final String fileName, final ZipOutputStream zos, final String originalFileName) throws IOException { // File is opened. final File file = new File(fileName); // File is renamed. final String newName = fileName.substring(fileName.indexOf(originalFileName), fileName.length()); final File renamedFile = new File(newName); file.renameTo(renamedFile); // File is included into ZIP. final FileInputStream fis = new FileInputStream(renamedFile); final ZipEntry zipEntry = new ZipEntry(newName); zos.putNextEntry(zipEntry); final byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { zos.write(bytes, 0, length); } // Closing elements. zos.closeEntry(); fis.close(); // File is deleted this.fileUtils.delete(newName); } }