Search in sources :

Example 1 with ZipException

use of net.lingala.zip4j.exception.ZipException in project ddf by codice.

the class ExportCommand method doContentExport.

private List<ExportItem> doContentExport(/*Mutable,IO*/
ZipFile zipFile, List<ExportItem> exportedItems) throws ZipException {
    List<ExportItem> contentItemsToExport = exportedItems.stream().filter(ei -> ei.getResourceUri() != null).filter(ei -> ei.getResourceUri().getScheme() != null).filter(ei -> ei.getResourceUri().getScheme().startsWith(ContentItem.CONTENT_SCHEME)).filter(ei -> !ei.getMetacardTag().equals("deleted")).filter(ei -> !ei.getMetacardTag().equals("revision") || ei.getResourceUri().getSchemeSpecificPart().equals(ei.getId())).filter(distinctByKey(ei -> ei.getResourceUri().getSchemeSpecificPart())).collect(Collectors.toList());
    List<ExportItem> exportedContentItems = new ArrayList<>();
    for (ExportItem contentItem : contentItemsToExport) {
        ResourceResponse resource;
        try {
            resource = catalogFramework.getLocalResource(new ResourceRequestByProductUri(contentItem.getResourceUri()));
        } catch (IOException | ResourceNotSupportedException e) {
            throw new CatalogCommandRuntimeException("Unable to retrieve resource for " + contentItem.getId(), e);
        } catch (ResourceNotFoundException e) {
            continue;
        }
        writeToZip(zipFile, contentItem, resource);
        exportedContentItems.add(contentItem);
        if (!contentItem.getMetacardTag().equals("revision")) {
            for (String derivedUri : contentItem.getDerivedUris()) {
                URI uri;
                try {
                    uri = new URI(derivedUri);
                } catch (URISyntaxException e) {
                    LOGGER.debug("Uri [{}] is not a valid URI. Derived content will not be included in export", derivedUri);
                    continue;
                }
                ResourceResponse derivedResource;
                try {
                    derivedResource = catalogFramework.getLocalResource(new ResourceRequestByProductUri(uri));
                } catch (IOException e) {
                    throw new CatalogCommandRuntimeException("Unable to retrieve resource for " + contentItem.getId(), e);
                } catch (ResourceNotFoundException | ResourceNotSupportedException e) {
                    LOGGER.warn("Could not retreive resource [{}]", uri, e);
                    console.printf("%sUnable to retrieve resource for export : %s%s%n", Ansi.ansi().fg(Ansi.Color.RED).toString(), uri, Ansi.ansi().reset().toString());
                    continue;
                }
                writeToZip(zipFile, contentItem, derivedResource);
            }
        }
    }
    return exportedContentItems;
}
Also used : Ansi(org.fusesource.jansi.Ansi) StringUtils(org.apache.commons.lang.StringUtils) DeleteStorageRequestImpl(ddf.catalog.content.operation.impl.DeleteStorageRequestImpl) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) BinaryContent(ddf.catalog.data.BinaryContent) LoggerFactory(org.slf4j.LoggerFactory) SecurityLogger(ddf.security.common.audit.SecurityLogger) Command(org.apache.karaf.shell.api.action.Command) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) MetacardVersion(ddf.catalog.core.versioning.MetacardVersion) Map(java.util.Map) ExportItem(org.codice.ddf.commands.catalog.export.ExportItem) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) URI(java.net.URI) ParseException(java.text.ParseException) ZipFile(net.lingala.zip4j.core.ZipFile) TimeZone(java.util.TimeZone) Predicate(java.util.function.Predicate) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DeletedMetacard(ddf.catalog.core.versioning.DeletedMetacard) Set(java.util.Set) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Objects(java.util.Objects) ZipException(net.lingala.zip4j.exception.ZipException) StorageException(ddf.catalog.content.StorageException) List(java.util.List) FilenameUtils(org.apache.commons.io.FilenameUtils) ResourceResponse(ddf.catalog.operation.ResourceResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) SimpleDateFormat(java.text.SimpleDateFormat) CatalogCommandRuntimeException(org.codice.ddf.commands.util.CatalogCommandRuntimeException) HashMap(java.util.HashMap) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Reference(org.apache.karaf.shell.api.action.lifecycle.Reference) SortBy(org.opengis.filter.sort.SortBy) ContentItem(ddf.catalog.content.data.ContentItem) Metacard(ddf.catalog.data.Metacard) CQLException(org.geotools.filter.text.cql2.CQLException) StorageProvider(ddf.catalog.content.StorageProvider) Result(ddf.catalog.data.Result) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Logger(org.slf4j.Logger) ZipParameters(net.lingala.zip4j.model.ZipParameters) QueryResultIterable(org.codice.ddf.commands.util.QueryResultIterable) IngestException(ddf.catalog.source.IngestException) ResourceRequestByProductUri(ddf.catalog.operation.impl.ResourceRequestByProductUri) IOException(java.io.IOException) IdAndUriMetacard(org.codice.ddf.commands.catalog.export.IdAndUriMetacard) File(java.io.File) JarSigner(org.codice.ddf.catalog.transformer.zip.JarSigner) TimeUnit(java.util.concurrent.TimeUnit) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) Paths(java.nio.file.Paths) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Filter(org.opengis.filter.Filter) Option(org.apache.karaf.shell.api.action.Option) Collections(java.util.Collections) ExportItem(org.codice.ddf.commands.catalog.export.ExportItem) ArrayList(java.util.ArrayList) CatalogCommandRuntimeException(org.codice.ddf.commands.util.CatalogCommandRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ResourceRequestByProductUri(ddf.catalog.operation.impl.ResourceRequestByProductUri) URI(java.net.URI) ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Example 2 with ZipException

use of net.lingala.zip4j.exception.ZipException in project RxTools by vondear.

the class RxZipTool method zipEncryptRargo.

public static String zipEncryptRargo(String src, String dest, boolean isCreateDir, String passwd, int unit) {
    File srcFile = new File(src);
    dest = buildDestinationZipFilePath(srcFile, dest);
    ZipParameters parameters = new ZipParameters();
    // 默认COMP_DEFLATE
    parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
    parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
    if (!RxDataTool.isNullString(passwd)) {
        parameters.setEncryptFiles(true);
        parameters.setEncryptionMethod(0);
        parameters.setPassword(passwd.toCharArray());
    }
    try {
        net.lingala.zip4j.core.ZipFile zipFile = new net.lingala.zip4j.core.ZipFile(dest);
        if (srcFile.isDirectory()) {
            if (!isCreateDir) {
                File[] subFiles = srcFile.listFiles();
                ArrayList<File> temp = new ArrayList();
                Collections.addAll(temp, subFiles);
                // zipFile.addFiles(temp, parameters);
                zipFile.createZipFile(temp, parameters, true, unit * 1000);
                return dest;
            }
            zipFile.createZipFileFromFolder(srcFile, parameters, true, unit * 1000);
            // 粗略的算一下分成多少份,获取的大小比实际的大点(一般是准确的)
            // 65536byte=64kb
            int partsize = (int) zipInfo(dest) / (unit);
            System.out.println("分割成功!总共分割成了" + (partsize + 1) + "个文件!");
        } else {
            zipFile.createZipFile(srcFile, parameters, true, unit * 1000);
        }
        return dest;
    } catch (ZipException var9) {
        var9.printStackTrace();
        return null;
    }
}
Also used : ArrayList(java.util.ArrayList) ZipException(net.lingala.zip4j.exception.ZipException) ZipParameters(net.lingala.zip4j.model.ZipParameters) ZipFile(java.util.zip.ZipFile) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 3 with ZipException

use of net.lingala.zip4j.exception.ZipException in project RxTools by vondear.

the class RxZipTool method zipEncrypt.

public static String zipEncrypt(String src, String dest, boolean isCreateDir, String passwd) {
    File srcFile = new File(src);
    dest = buildDestinationZipFilePath(srcFile, dest);
    ZipParameters parameters = new ZipParameters();
    // 压缩方式
    parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
    // 压缩级别
    parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
    if (!RxDataTool.isNullString(passwd)) {
        parameters.setEncryptFiles(true);
        // 加密方式
        parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
        parameters.setPassword(passwd.toCharArray());
    }
    try {
        net.lingala.zip4j.core.ZipFile zipFile = new net.lingala.zip4j.core.ZipFile(dest);
        if (srcFile.isDirectory()) {
            // 如果不创建目录的话,将直接把给定目录下的文件压缩到压缩文件,即没有目录结构
            if (!isCreateDir) {
                File[] subFiles = srcFile.listFiles();
                ArrayList<File> temp = new ArrayList<File>();
                Collections.addAll(temp, subFiles);
                zipFile.addFiles(temp, parameters);
                return dest;
            }
            zipFile.addFolder(srcFile, parameters);
        } else {
            zipFile.addFile(srcFile, parameters);
        }
        return dest;
    } catch (ZipException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : ZipFile(java.util.zip.ZipFile) ArrayList(java.util.ArrayList) ZipException(net.lingala.zip4j.exception.ZipException) File(java.io.File) ZipFile(java.util.zip.ZipFile) ZipParameters(net.lingala.zip4j.model.ZipParameters)

Example 4 with ZipException

use of net.lingala.zip4j.exception.ZipException in project knox by apache.

the class GatewayServer method internalActivateArchive.

private synchronized void internalActivateArchive(Topology topology, File warDir) throws IOException, ZipException, ParserConfigurationException, TransformerException, SAXException {
    log.activatingTopologyArchive(topology.getName(), warDir.getName());
    try {
        WebAppContext newContext = createWebAppContext(topology, warDir, Urls.decode(warDir.getName()));
        WebAppContext oldContext = deployments.get(newContext.getContextPath());
        deployments.put(newContext.getContextPath(), newContext);
        if (oldContext != null) {
            contexts.removeHandler(oldContext);
        }
        contexts.addHandler(newContext);
        if (contexts.isRunning() && !newContext.isRunning()) {
            newContext.start();
        }
    } catch (Exception e) {
        auditor.audit(Action.DEPLOY, topology.getName(), ResourceType.TOPOLOGY, ActionOutcome.FAILURE);
        log.failedToDeployTopology(topology.getName(), e);
    }
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) DeploymentException(org.apache.knox.gateway.deploy.DeploymentException) URISyntaxException(java.net.URISyntaxException) KeyStoreException(java.security.KeyStoreException) ZipException(net.lingala.zip4j.exception.ZipException) ParseException(org.apache.commons.cli.ParseException) SAXException(org.xml.sax.SAXException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) UnknownHostException(java.net.UnknownHostException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 5 with ZipException

use of net.lingala.zip4j.exception.ZipException in project codeforces-commons by Codeforces.

the class ZipUtil method formatZipArchiveContentForView.

/**
 * Formats content of the ZIP-archive for view and returns result as UTF-8 bytes. The {@code truncated} flag
 * indicates that the length of returned view was restricted by {@code maxLength} parameter.
 *
 * @param zipFile                              ZIP-archive to format
 * @param maxLength                            maximal allowed length of result
 * @param maxEntryLineCount                    maximal allowed number of content lines to display for a single ZIP-archive entry
 * @param maxEntryLineLength                   maximal allowed length of ZIP-archive entry content line
 * @param entryListHeaderPattern               pattern of entry list header; parameters: {@code fileName}, {@code filePath}, {@code entryCount}
 * @param entryListItemPattern                 pattern of entry list item; parameters: {@code entryName}, {@code entrySize}, {@code entryIndex} (1-based)
 * @param entryListItemSeparatorPattern        pattern of entry list separator
 * @param entryListCloserPattern               pattern of entry list closer; parameters: {@code fileName}, {@code filePath}
 * @param entryContentHeaderPattern            pattern of entry content header; parameters: {@code entryName}, {@code entrySize}
 * @param entryContentLinePattern              pattern of entry content line; parameters: {@code entryLine}
 * @param entryContentLineSeparatorPattern     pattern of entry content separator
 * @param entryContentCloserPattern            pattern of entry content closer; parameters: {@code entryName}
 * @param binaryEntryContentPlaceholderPattern pattern of binary entry content placeholder; parameters: {@code entrySize}
 * @param emptyZipFilePlaceholderPattern       pattern of empty (no entries) ZIP-file placeholder; parameters: {@code fileName}, {@code filePath}
 * @return formatted view of ZIP-archive
 * @throws IOException if {@code zipFile} is not a correct ZIP-archive or any other I/O-error has been occured
 * @see String#format(String, Object...)
 */
@SuppressWarnings("OverlyLongMethod")
@Nonnull
public static FileUtil.FirstBytes formatZipArchiveContentForView(File zipFile, int maxLength, int maxEntryLineCount, int maxEntryLineLength, @Nullable String entryListHeaderPattern, @Nullable String entryListItemPattern, @Nullable String entryListItemSeparatorPattern, @Nullable String entryListCloserPattern, @Nullable String entryContentHeaderPattern, @Nullable String entryContentLinePattern, @Nullable String entryContentLineSeparatorPattern, @Nullable String entryContentCloserPattern, @Nullable String binaryEntryContentPlaceholderPattern, @Nullable String emptyZipFilePlaceholderPattern) throws IOException {
    entryListHeaderPattern = StringUtil.nullToDefault(entryListHeaderPattern, "ZIP-file entries {\n");
    entryListItemPattern = StringUtil.nullToDefault(entryListItemPattern, "    %3$03d. %1$s (%2$d B)");
    entryListItemSeparatorPattern = StringUtil.nullToDefault(entryListItemSeparatorPattern, "\n");
    entryListCloserPattern = StringUtil.nullToDefault(entryListCloserPattern, "\n}\n\n");
    entryContentHeaderPattern = StringUtil.nullToDefault(entryContentHeaderPattern, "Entry %1$s (%2$d B) {\n");
    entryContentLinePattern = StringUtil.nullToDefault(entryContentLinePattern, "    %1$s");
    entryContentLineSeparatorPattern = StringUtil.nullToDefault(entryContentLineSeparatorPattern, "\n");
    entryContentCloserPattern = StringUtil.nullToDefault(entryContentCloserPattern, "\n} // %1$s\n\n");
    binaryEntryContentPlaceholderPattern = StringUtil.nullToDefault(binaryEntryContentPlaceholderPattern, "    *** BINARY DATA (%1$d B) ***");
    emptyZipFilePlaceholderPattern = StringUtil.nullToDefault(emptyZipFilePlaceholderPattern, "Empty ZIP-file.");
    try {
        Charset charset = StandardCharsets.UTF_8;
        ZipFile internalZipFile = new ZipFile(zipFile);
        List<?> fileHeaders = internalZipFile.getFileHeaders();
        int headerCount = fileHeaders.size();
        if (headerCount <= 0) {
            return formatEmptyZipFilePlaceholder(zipFile, maxLength, emptyZipFilePlaceholderPattern, charset);
        }
        MutableBoolean truncated = new MutableBoolean(Boolean.FALSE);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        CountingOutputStream countingOutputStream = new CountingOutputStream(byteArrayOutputStream);
        byte[] entryListHeaderBytes = String.format(entryListHeaderPattern, zipFile.getName(), zipFile.getPath(), headerCount).getBytes(charset);
        if (!writeBytesForView(countingOutputStream, entryListHeaderBytes, maxLength, truncated)) {
            throw new IllegalArgumentException(String.format("Argument 'maxLength' (%d) is less than the length of entry list header '%s' (%d bytes).", maxLength, new String(entryListHeaderBytes, charset), entryListHeaderBytes.length));
        }
        fileHeaders.sort(Comparator.comparing(header -> ((FileHeader) header).getFileName()));
        for (int headerIndex = 0; headerIndex < headerCount; ++headerIndex) {
            FileHeader header = (FileHeader) fileHeaders.get(headerIndex);
            String fileName = header.getFileName();
            String entryListItemAppendix = headerIndex == headerCount - 1 ? String.format(entryListCloserPattern, zipFile.getName(), zipFile.getPath()) : entryListItemSeparatorPattern;
            byte[] entryListItemBytes = (String.format(entryListItemPattern, fileName, header.getUncompressedSize(), headerIndex + 1) + entryListItemAppendix).getBytes(charset);
            if (!writeBytesForView(countingOutputStream, entryListItemBytes, maxLength, truncated)) {
                break;
            }
        }
        for (int headerIndex = 0; headerIndex < headerCount; ++headerIndex) {
            FileHeader header = (FileHeader) fileHeaders.get(headerIndex);
            if (header.isDirectory()) {
                continue;
            }
            formatAndAppendEntryContent(countingOutputStream, maxLength, truncated, charset, internalZipFile, header, maxEntryLineCount, maxEntryLineLength, entryContentHeaderPattern, entryContentLinePattern, entryContentLineSeparatorPattern, entryContentCloserPattern, binaryEntryContentPlaceholderPattern);
            if (truncated.booleanValue()) {
                break;
            }
        }
        return new FileUtil.FirstBytes(truncated.booleanValue(), byteArrayOutputStream.toByteArray());
    } catch (ZipException e) {
        throw new IOException("Can't format ZIP-file for view.", e);
    }
}
Also used : ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) java.util(java.util) IOCase(org.apache.commons.io.IOCase) java.util.zip(java.util.zip) ByteArrayOutputStream(com.codeforces.commons.io.ByteArrayOutputStream) Charset(java.nio.charset.Charset) StringUtil(com.codeforces.commons.text.StringUtil) NameFileFilter(org.apache.commons.io.filefilter.NameFileFilter) Math.max(com.codeforces.commons.math.Math.max) Nonnull(javax.annotation.Nonnull) FileHeader(net.lingala.zip4j.model.FileHeader) Nullable(javax.annotation.Nullable) ZipFile(net.lingala.zip4j.core.ZipFile) ZipParameters(net.lingala.zip4j.model.ZipParameters) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) com.codeforces.commons.io(com.codeforces.commons.io) StandardCharsets(java.nio.charset.StandardCharsets) ZipException(net.lingala.zip4j.exception.ZipException) IOUtils(org.apache.commons.io.IOUtils) Contract(org.jetbrains.annotations.Contract) FsSyncException(de.schlichtherle.truezip.fs.FsSyncException) java.io(java.io) de.schlichtherle.truezip.file(de.schlichtherle.truezip.file) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) Patterns(com.codeforces.commons.text.Patterns) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) Charset(java.nio.charset.Charset) ZipException(net.lingala.zip4j.exception.ZipException) ByteArrayOutputStream(com.codeforces.commons.io.ByteArrayOutputStream) ZipFile(net.lingala.zip4j.core.ZipFile) FileHeader(net.lingala.zip4j.model.FileHeader) Nonnull(javax.annotation.Nonnull)

Aggregations

ZipException (net.lingala.zip4j.exception.ZipException)24 ZipFile (net.lingala.zip4j.core.ZipFile)15 File (java.io.File)13 ZipParameters (net.lingala.zip4j.model.ZipParameters)9 FileHeader (net.lingala.zip4j.model.FileHeader)8 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)5 ZipFile (net.lingala.zip4j.ZipFile)3 WorldException (com.voxelgameslib.voxelgameslib.exception.WorldException)2 BinaryContent (ddf.catalog.data.BinaryContent)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 RandomAccessFile (java.io.RandomAccessFile)2 URISyntaxException (java.net.URISyntaxException)2 ZipFile (java.util.zip.ZipFile)2 HeaderReader (net.lingala.zip4j.core.HeaderReader)2 LocalFileHeader (net.lingala.zip4j.model.LocalFileHeader)2 ZipModel (net.lingala.zip4j.model.ZipModel)2 UnzipEngine (net.lingala.zip4j.unzip.UnzipEngine)2 FileMeta (org.molgenis.data.file.model.FileMeta)2