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;
}
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;
}
}
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;
}
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);
}
}
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);
}
}
Aggregations