Search in sources :

Example 1 with FileHeader

use of net.lingala.zip4j.model.FileHeader in project v7files by thiloplanz.

the class ZipFile method extractFile.

/**
	 * find the data indicated by the ContentPointer, treats it as a zip
	 * archive, extracts the named file inside the archive, stores a reference
	 * to it in the ContentStorage and returns a ContentPointer to it.
	 * 
	 * @throws FileNotFoundException
	 *             if the archive exists, but does not contain the named file
	 * @throws IllegalArgumentException
	 *             if the ContentPointer does not refer to a zip archive
	 * 
	 */
public static final ContentPointer extractFile(ContentStorage storage, ContentPointer zipFile, String fileName) throws IOException {
    Content zip = storage.getContent(zipFile);
    if (zip == null)
        throw new IllegalArgumentException("invalid ContentPointer " + zipFile);
    File tmp = File.createTempFile("v7files_zipfile_extractfile_", ".zip");
    try {
        OutputStream f = new FileOutputStream(tmp);
        IOUtils.copy(zip.getInputStream(), f);
        f.close();
        // open up the zip file
        HeaderReader r = new HeaderReader(new RandomAccessFile(tmp, "r"));
        ZipModel model = r.readAllHeaders();
        model.setZipFile(tmp.getAbsolutePath());
        List<?> fhs = model.getCentralDirectory().getFileHeaders();
        for (Object _fh : fhs) {
            FileHeader fh = (FileHeader) _fh;
            if (fileName.equals(fh.getFileName())) {
                UnzipEngine en = new UnzipEngine(model, fh);
                // this will read the local file header
                en.getInputStream();
                LocalFileHeader lh = en.getLocalFileHeader();
                return store(storage, zipFile.serialize(), fh, lh);
            }
        }
    } catch (ZipException e) {
        throw new IllegalArgumentException("ContentPointer does not refer to a zip file: " + zipFile, e);
    } finally {
        tmp.delete();
    }
    throw new FileNotFoundException("ContentPointer does not contain " + fileName + ": " + zipFile);
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) UnzipEngine(net.lingala.zip4j.unzip.UnzipEngine) FileNotFoundException(java.io.FileNotFoundException) ZipException(net.lingala.zip4j.exception.ZipException) LocalFileHeader(net.lingala.zip4j.model.LocalFileHeader) ZipModel(net.lingala.zip4j.model.ZipModel) RandomAccessFile(java.io.RandomAccessFile) InlineContent(v7db.files.spi.InlineContent) Content(v7db.files.spi.Content) FileOutputStream(java.io.FileOutputStream) HeaderReader(net.lingala.zip4j.core.HeaderReader) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) LocalFileHeader(net.lingala.zip4j.model.LocalFileHeader) FileHeader(net.lingala.zip4j.model.FileHeader)

Example 2 with FileHeader

use of net.lingala.zip4j.model.FileHeader in project v7files by thiloplanz.

the class ZipFile method index.

/**
	 * index all individual files found in a zip archive already in storage
	 * 
	 * @throws IOException
	 */
public static final void index(ContentStorage storage, ContentPointer zipFile) throws IOException {
    Content zip = storage.getContent(zipFile);
    if (zip == null)
        throw new IllegalArgumentException("invalid ContentPointer " + zipFile);
    File tmp = File.createTempFile("v7files_zipfile_extractfile_", ".zip");
    try {
        OutputStream f = new FileOutputStream(tmp);
        IOUtils.copy(zip.getInputStream(), f);
        f.close();
        // open up the zip file
        HeaderReader r = new HeaderReader(new RandomAccessFile(tmp, "r"));
        ZipModel model = r.readAllHeaders();
        model.setZipFile(tmp.getAbsolutePath());
        Map<String, Object> map = zipFile.serialize();
        List<?> fhs = model.getCentralDirectory().getFileHeaders();
        for (Object _fh : fhs) {
            FileHeader fh = (FileHeader) _fh;
            UnzipEngine en = new UnzipEngine(model, fh);
            // this will read the local file header
            en.getInputStream();
            LocalFileHeader lh = en.getLocalFileHeader();
            store(storage, map, fh, lh);
        }
    } catch (ZipException e) {
        throw new IllegalArgumentException("ContentPointer does not refer to a zip file: " + zipFile, e);
    } finally {
        tmp.delete();
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) UnzipEngine(net.lingala.zip4j.unzip.UnzipEngine) ZipException(net.lingala.zip4j.exception.ZipException) LocalFileHeader(net.lingala.zip4j.model.LocalFileHeader) ZipModel(net.lingala.zip4j.model.ZipModel) RandomAccessFile(java.io.RandomAccessFile) InlineContent(v7db.files.spi.InlineContent) Content(v7db.files.spi.Content) FileOutputStream(java.io.FileOutputStream) HeaderReader(net.lingala.zip4j.core.HeaderReader) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) LocalFileHeader(net.lingala.zip4j.model.LocalFileHeader) FileHeader(net.lingala.zip4j.model.FileHeader)

Example 3 with FileHeader

use of net.lingala.zip4j.model.FileHeader in project tdi-studio-se by Talend.

the class Unzip method doUnzipWithAes.

// zip4j compress&decryption impl
public void doUnzipWithAes() throws Exception {
    File file = new File(sourceZip);
    if (password == null || "".equals(password)) {
        // To make sure the System.out.println message
        Thread.sleep(1000);
        // come before
        throw new RuntimeException("Please enter the password and try again..");
    }
    ZipFile zipFile = new ZipFile(sourceZip);
    if (checkArchive) {
        if (!zipFile.isValidZipFile()) {
            throw new RuntimeException("The file " + sourceZip + " is corrupted, process terminated...");
        }
    }
    if (zipFile.isEncrypted()) {
        zipFile.setPassword(password);
    }
    List fileHeaderList = zipFile.getFileHeaders();
    if (fileHeaderList == null) {
        return;
    }
    for (int i = 0; i < fileHeaderList.size(); i++) {
        FileHeader fileHeader = (FileHeader) fileHeaderList.get(i);
        String filename = fileHeader.getFileName();
        if (verbose) {
            System.out.println("Source file  : " + filename);
        }
        if (!extractPath) {
            filename = filename.replaceAll("\\\\", "/");
            filename = filename.substring(filename.lastIndexOf('/') + 1);
        }
        zipFile.extractFile(fileHeader, targetDir, null, filename);
    }
}
Also used : ZipFile(net.lingala.zip4j.core.ZipFile) List(java.util.List) ZipFile(net.lingala.zip4j.core.ZipFile) File(java.io.File) FileHeader(net.lingala.zip4j.model.FileHeader)

Aggregations

File (java.io.File)3 FileHeader (net.lingala.zip4j.model.FileHeader)3 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 RandomAccessFile (java.io.RandomAccessFile)2 HeaderReader (net.lingala.zip4j.core.HeaderReader)2 ZipException (net.lingala.zip4j.exception.ZipException)2 LocalFileHeader (net.lingala.zip4j.model.LocalFileHeader)2 ZipModel (net.lingala.zip4j.model.ZipModel)2 UnzipEngine (net.lingala.zip4j.unzip.UnzipEngine)2 Content (v7db.files.spi.Content)2 InlineContent (v7db.files.spi.InlineContent)2 FileNotFoundException (java.io.FileNotFoundException)1 List (java.util.List)1 ZipFile (net.lingala.zip4j.core.ZipFile)1