Search in sources :

Example 16 with ZipEntry

use of org.apache.tools.zip.ZipEntry in project translationstudio8 by heartsome.

the class ZipFileExporter2 method write.

/**
	 * Write the passed resource to the current archive.
	 * @param resource
	 *            org.eclipse.core.resources.IFile
	 * @param destinationPath
	 *            java.lang.String
	 * @exception java.io.IOException
	 * @exception org.eclipse.core.runtime.CoreException
	 */
public void write(IFile resource, String destinationPath) throws IOException, CoreException {
    ZipEntry newEntry = new ZipEntry(destinationPath);
    write(newEntry, resource);
}
Also used : ZipEntry(org.apache.tools.zip.ZipEntry)

Example 17 with ZipEntry

use of org.apache.tools.zip.ZipEntry in project translationstudio8 by heartsome.

the class ZipFileExporter2 method addDbFile.

public void addDbFile(File file, String destinationPath) throws IOException {
    ZipEntry newEntry = new ZipEntry(destinationPath);
    writeFile(newEntry, file);
}
Also used : ZipEntry(org.apache.tools.zip.ZipEntry)

Example 18 with ZipEntry

use of org.apache.tools.zip.ZipEntry in project tdi-studio-se by Talend.

the class ZipFileUtils method unZip.

public static String unZip(String unZipFileName, String destFileName) {
    File unzipFile = new File(unZipFileName);
    if (destFileName == null || destFileName.trim().length() == 0) {
        destFileName = unzipFile.getParent();
    }
    File destFile;
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(unzipFile, "GBK");
        for (Enumeration entries = zipFile.getEntries(); entries.hasMoreElements(); ) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            destFile = new File(destFileName, entry.getName());
            unZipFile(destFile, zipFile, entry);
        }
    } catch (Exception e) {
        ExceptionHandler.process(e);
        return e.getMessage();
    } finally {
        try {
            assert zipFile != null;
            zipFile.close();
        } catch (Exception e) {
            ExceptionHandler.process(e);
        }
    }
    return null;
}
Also used : Enumeration(java.util.Enumeration) ZipFile(org.apache.tools.zip.ZipFile) ZipEntry(org.apache.tools.zip.ZipEntry) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File) IOException(java.io.IOException)

Example 19 with ZipEntry

use of org.apache.tools.zip.ZipEntry in project PublicCMS-preview by sanluan.

the class ZipUtils method unzip.

/**
 * @param zipFilePath
 * @param targetPath
 * @param overwrite
 * @throws IOException
 */
public static void unzip(String zipFilePath, String targetPath, boolean overwrite) throws IOException {
    ZipFile zipFile = new ZipFile(zipFilePath, ENCODING);
    Enumeration<? extends ZipEntry> entryEnum = zipFile.getEntries();
    if (null != entryEnum) {
        while (entryEnum.hasMoreElements()) {
            ZipEntry zipEntry = entryEnum.nextElement();
            if (zipEntry.isDirectory()) {
                File dir = new File(targetPath + File.separator + zipEntry.getName());
                dir.mkdirs();
            } else {
                File targetFile = new File(targetPath + File.separator + zipEntry.getName());
                if (!targetFile.exists() || overwrite) {
                    targetFile.getParentFile().mkdirs();
                    try (InputStream inputStream = zipFile.getInputStream(zipEntry);
                        FileOutputStream outputStream = new FileOutputStream(targetFile);
                        FileLock fileLock = outputStream.getChannel().tryLock()) {
                        StreamUtils.copy(inputStream, outputStream);
                    }
                }
            }
        }
    }
    zipFile.close();
}
Also used : ZipFile(org.apache.tools.zip.ZipFile) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(org.apache.tools.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) FileLock(java.nio.channels.FileLock) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File)

Example 20 with ZipEntry

use of org.apache.tools.zip.ZipEntry in project PublicCMS-preview by sanluan.

the class ZipUtils method compress.

/**
 * @param file
 * @param out
 * @param basedir
 * @throws IOException
 */
private static void compress(Path sourceFilePath, ZipOutputStream out, String basedir) throws IOException {
    if (Files.isDirectory(sourceFilePath)) {
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(sourceFilePath)) {
            for (Path entry : stream) {
                BasicFileAttributes attrs = Files.readAttributes(entry, BasicFileAttributes.class);
                String fullName = BLANK.equals(basedir) ? entry.toFile().getName() : basedir + SEPARATOR + entry.toFile().getName();
                if (attrs.isDirectory()) {
                    ZipEntry zipEntry = new ZipEntry(fullName + SEPARATOR);
                    out.putNextEntry(zipEntry);
                    compress(entry, out, fullName);
                } else {
                    compressFile(entry.toFile(), out, fullName);
                }
            }
        } catch (IOException e) {
        }
    } else {
        compressFile(sourceFilePath.toFile(), out, sourceFilePath.toFile().getName());
    }
}
Also used : Path(java.nio.file.Path) ZipEntry(org.apache.tools.zip.ZipEntry) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

ZipEntry (org.apache.tools.zip.ZipEntry)32 ZipFile (org.apache.tools.zip.ZipFile)17 File (java.io.File)13 FileOutputStream (java.io.FileOutputStream)9 IOException (java.io.IOException)9 FileInputStream (java.io.FileInputStream)7 BufferedInputStream (java.io.BufferedInputStream)6 InputStream (java.io.InputStream)6 BufferedOutputStream (java.io.BufferedOutputStream)5 Enumeration (java.util.Enumeration)5 ZipException (java.util.zip.ZipException)4 BuildException (org.apache.tools.ant.BuildException)4 ZipOutputStream (org.apache.tools.zip.ZipOutputStream)4 OutputStream (java.io.OutputStream)3 ZipResource (org.apache.tools.ant.types.resources.ZipResource)3 FileNotFoundException (java.io.FileNotFoundException)2 RandomAccessFile (java.io.RandomAccessFile)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Date (java.util.Date)2 ZipOutputStream (java.util.zip.ZipOutputStream)2