use of net.lingala.zip4j.ZipFile in project ArachneCentralAPI by OHDSI.
the class BaseSubmissionServiceImpl method unzipWithNested.
private void unzipWithNested(final ZipFile zipFile, final Path unzipDir) throws IOException {
final Queue<Triple<Path, ZipFile, FileHeader>> queue = zipFile.getFileHeaders().stream().filter(fh -> !fh.isDirectory()).map(fh -> Triple.of(unzipDir, zipFile, fh)).collect(Collectors.toCollection(LinkedList::new));
final Set<File> filesToDelete = new HashSet<>();
while (!queue.isEmpty()) {
final Triple<Path, ZipFile, FileHeader> element = queue.poll();
final Path unzipPath = element.getLeft();
final ZipFile zip = element.getMiddle();
final FileHeader fileHeader = element.getRight();
final String relativeFilePath = fileHeader.getFileName();
final String relativePath = FilenameUtils.getPath(relativeFilePath);
if (isNotBlank(relativePath)) {
unzipPath.resolve(relativePath).toFile().mkdirs();
}
final File localFile = unzipPath.resolve(relativeFilePath).toFile();
FileUtils.copyInputStreamToFile(zip.getInputStream(fileHeader), localFile);
if (relativeFilePath.endsWith(".zip")) {
final ZipFile innerZipFile = new ZipFile(localFile);
if (innerZipFile.isValidZipFile()) {
final String relativePathWithoutExtension = FilenameUtils.removeExtension(relativeFilePath);
final Path zipFileNamedDir = unzipPath.resolve(relativePathWithoutExtension);
queue.addAll(innerZipFile.getFileHeaders().stream().filter(fh -> !fh.isDirectory()).map(fh -> Triple.of(zipFileNamedDir, innerZipFile, fh)).collect(Collectors.toCollection(LinkedList::new)));
filesToDelete.add(localFile);
}
}
}
// delete unzipped zip files
for (final File file : filesToDelete) {
FileUtils.deleteQuietly(file);
}
}
use of net.lingala.zip4j.ZipFile in project irontest by zheng-wang.
the class XMLValidAgainstXSDAssertionVerifier method verify.
/**
* @param inputs contains only one argument: the XML string that the assertion is verifying against the XSD
* @return
* @throws Exception
*/
@Override
public AssertionVerificationResult verify(Object... inputs) throws Exception {
String xmlString = (String) inputs[0];
XMLValidAgainstXSDAssertionProperties assertionProperties = (XMLValidAgainstXSDAssertionProperties) getAssertion().getOtherProperties();
String fileName = StringUtils.trimToEmpty(assertionProperties.getFileName());
// validate arguments
if (xmlString == null) {
throw new IllegalArgumentException("XML is null.");
} else if ("".equals(fileName)) {
throw new IllegalArgumentException("XSD file not uploaded.");
} else if (!(fileName.toLowerCase().endsWith(".xsd") || fileName.toLowerCase().endsWith(".zip"))) {
throw new IllegalArgumentException("Unrecognized XSD file format.");
}
XMLValidAgainstXSDAssertionVerificationResult result = new XMLValidAgainstXSDAssertionVerificationResult();
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema;
Path tempDir = null;
if (fileName.toLowerCase().endsWith(".xsd")) {
schema = factory.newSchema(new StreamSource(new ByteArrayInputStream(assertionProperties.getFileBytes())));
} else {
// the XSD(s) are in a zip file
// extract the zip file in a temp directory
tempDir = Files.createTempDirectory("irontest");
File zipFile = new File(tempDir.toFile(), assertionProperties.getFileName());
FileUtils.writeByteArrayToFile(zipFile, assertionProperties.getFileBytes());
new ZipFile(zipFile).extractAll(tempDir.toString());
// create schema object out of the extracted XSD files
Collection<File> xsdFiles = FileUtils.listFiles(tempDir.toFile(), new String[] { "xsd", "XSD" }, true);
List<Source> sourceList = new ArrayList<>();
for (File xsdFile : xsdFiles) {
sourceList.add(new StreamSource(xsdFile));
}
schema = factory.newSchema(sourceList.toArray(new Source[sourceList.size()]));
}
Validator validator = schema.newValidator();
try {
validator.validate(new StreamSource(new StringReader(xmlString)));
result.setResult(TestResult.PASSED);
} catch (SAXParseException e) {
result.setResult(TestResult.FAILED);
result.setFailureDetails(e.toString());
} finally {
if (tempDir != null) {
FileUtils.deleteDirectory(tempDir.toFile());
}
}
return result;
}
use of net.lingala.zip4j.ZipFile in project yyl_example by Relucent.
the class Zip4JExample method zip.
/**
* 压缩文件
*/
private static void zip(File file) throws ZipException, IOException {
ZipFile zipFile = new ZipFile(file);
ZipParameters parameters = new ZipParameters();
// 压缩方式
parameters.setCompressionMethod(CompressionMethod.DEFLATE);
// 压缩级别
parameters.setCompressionLevel(CompressionLevel.NORMAL);
// 加密等级
parameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_128);
// 加密
parameters.setEncryptFiles(true);
// 加密方法
parameters.setEncryptionMethod(EncryptionMethod.AES);
// 设置密码
zipFile.setPassword(PASSWORD);
try (InputStream input = toInputStream("江南可采莲,莲叶何田田。鱼戏莲叶间。鱼戏莲叶东,鱼戏莲叶西,鱼戏莲叶南,鱼戏莲叶北。 ")) {
parameters.setFileNameInZip("汉乐府/江南.txt");
zipFile.addStream(input, parameters);
}
try (InputStream input = toInputStream("千山鸟飞绝,万径人踪灭。\n孤舟蓑笠翁,独钓寒江雪。 ")) {
parameters.setFileNameInZip("江雪.txt");
zipFile.addStream(input, parameters);
}
try (InputStream input = toInputStream("岱宗夫如何?齐鲁青未了。\n造化钟神秀,阴阳割昏晓。\n荡胸生曾云,决眦入归鸟。\n会当凌绝顶,一览众山小。")) {
parameters.setFileNameInZip("杜甫/望岳.txt");
zipFile.addStream(input, parameters);
}
try (InputStream input = toInputStream("天门中断楚江开,碧水东流至此回。\n两岸青山相对出,孤帆一片日边来。 ")) {
parameters.setFileNameInZip("李白/望天门山.txt");
zipFile.addStream(input, parameters);
}
try (InputStream input = toInputStream("日照香炉生紫烟,遥看瀑布挂前川。\n飞流直下三千尺,疑是银河落九天。 ")) {
parameters.setFileNameInZip("李白/望庐山瀑布.txt");
zipFile.addStream(input, parameters);
}
System.out.println("zipPath: " + file.getAbsolutePath());
}
use of net.lingala.zip4j.ZipFile in project J2ME-Loader by nikita36078.
the class AppInstaller method loadManifest.
private Descriptor loadManifest(File jar) throws IOException {
ZipFile zip = new ZipFile(jar);
FileHeader manifest = zip.getFileHeader(JarFile.MANIFEST_NAME);
if (manifest == null)
throw new IOException("JAR not have " + JarFile.MANIFEST_NAME);
try (ZipInputStream is = zip.getInputStream(manifest)) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(20480);
byte[] buf = new byte[4096];
int read;
while ((read = is.read(buf)) != -1) {
baos.write(buf, 0, read);
}
return new Descriptor(baos.toString(), false);
}
}
use of net.lingala.zip4j.ZipFile in project J2ME-Loader by nikita36078.
the class AndroidProducer method processJar.
public static void processJar(File jarInputFile, File jarOutputFile) throws IOException {
HashMap<String, byte[]> resources = new HashMap<>();
ZipEntry zipEntry;
InputStream zis;
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(jarOutputFile))) {
ZipFile zip = new ZipFile(jarInputFile);
for (FileHeader header : zip.getFileHeaders()) {
// Some zip entries have zero length names
if (header.getFileNameLength() > 0 && !header.isDirectory()) {
zis = zip.getInputStream(header);
String name = header.getFileName();
byte[] inBuffer = IOUtils.toByteArray(zis);
resources.put(name, inBuffer);
zis.close();
}
}
for (String name : resources.keySet()) {
byte[] inBuffer = resources.get(name);
byte[] outBuffer = inBuffer;
try {
if (name.endsWith(".class")) {
outBuffer = instrument(inBuffer, name.replace(".class", ""));
}
zos.putNextEntry(new ZipEntry(name));
zos.write(outBuffer);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Aggregations