Search in sources :

Example 6 with ZipFile

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);
    }
}
Also used : Triple(org.apache.commons.lang3.tuple.Triple) Arrays(java.util.Arrays) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) ResultFile(com.odysseusinc.arachne.portal.model.ResultFile) FileSaveRequest(com.odysseusinc.arachne.storage.util.FileSaveRequest) FAILED_PUBLISHED(com.odysseusinc.arachne.portal.model.SubmissionStatus.FAILED_PUBLISHED) BaseDataSourceService(com.odysseusinc.arachne.portal.service.BaseDataSourceService) Analysis(com.odysseusinc.arachne.portal.model.Analysis) Map(java.util.Map) Sort(org.springframework.data.domain.Sort) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) Triple(org.apache.commons.lang3.tuple.Triple) Path(java.nio.file.Path) SubmissionStatusHistoryElement(com.odysseusinc.arachne.portal.model.SubmissionStatusHistoryElement) SubmissionHelper(com.odysseusinc.arachne.portal.util.SubmissionHelper) EXECUTE(com.odysseusinc.arachne.portal.service.impl.submission.SubmissionActionType.EXECUTE) ArachneMailSender(com.odysseusinc.arachne.portal.service.mail.ArachneMailSender) BaseSubmissionRepository(com.odysseusinc.arachne.portal.repository.submission.BaseSubmissionRepository) SubmissionGroupRepository(com.odysseusinc.arachne.portal.repository.SubmissionGroupRepository) PageRequest(org.springframework.data.domain.PageRequest) Set(java.util.Set) Page(org.springframework.data.domain.Page) IUser(com.odysseusinc.arachne.portal.model.IUser) SubmissionInsightRepository(com.odysseusinc.arachne.portal.repository.SubmissionInsightRepository) SimpMessagingTemplate(org.springframework.messaging.simp.SimpMessagingTemplate) Stream(java.util.stream.Stream) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) CollectionUtils(org.springframework.util.CollectionUtils) SubmissionStatus(com.odysseusinc.arachne.portal.model.SubmissionStatus) ZipOutputStream(java.util.zip.ZipOutputStream) PENDING(com.odysseusinc.arachne.portal.model.SubmissionStatus.PENDING) FAILED_REJECTED(com.odysseusinc.arachne.portal.model.SubmissionStatus.FAILED_REJECTED) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) InvitationApprovalSubmissionArachneMailMessage(com.odysseusinc.arachne.portal.service.mail.InvitationApprovalSubmissionArachneMailMessage) ApproveDTO(com.odysseusinc.arachne.portal.api.v1.dto.ApproveDTO) ZipUtil(com.odysseusinc.arachne.portal.util.ZipUtil) UserService(com.odysseusinc.arachne.portal.service.UserService) FileHeader(net.lingala.zip4j.model.FileHeader) SubmissionFileRepository(com.odysseusinc.arachne.portal.repository.SubmissionFileRepository) Files(java.nio.file.Files) SubmissionGroupSpecification(com.odysseusinc.arachne.portal.model.search.SubmissionGroupSpecification) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) EntityManager(javax.persistence.EntityManager) DataNodeUtils(com.odysseusinc.arachne.portal.util.DataNodeUtils) File(java.io.File) ContentStorageHelper(com.odysseusinc.arachne.portal.util.ContentStorageHelper) AnalysisFile(com.odysseusinc.arachne.portal.model.AnalysisFile) QuerySpec(com.odysseusinc.arachne.storage.model.QuerySpec) Paths(java.nio.file.Paths) FAILED(com.odysseusinc.arachne.portal.model.SubmissionStatus.FAILED) PUBLISH(com.odysseusinc.arachne.portal.service.impl.submission.SubmissionActionType.PUBLISH) DataNodeUtils.isDataNodeOwner(com.odysseusinc.arachne.portal.util.DataNodeUtils.isDataNodeOwner) UpdateNotificationDTO(com.odysseusinc.arachne.portal.api.v1.dto.UpdateNotificationDTO) AnalysisHelper(com.odysseusinc.arachne.portal.util.AnalysisHelper) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) EXECUTED_PUBLISHED(com.odysseusinc.arachne.portal.model.SubmissionStatus.EXECUTED_PUBLISHED) IDataSource(com.odysseusinc.arachne.portal.model.IDataSource) ResultFileSearch(com.odysseusinc.arachne.portal.model.search.ResultFileSearch) SubmissionFile(com.odysseusinc.arachne.portal.model.SubmissionFile) SubmissionStatusHistoryRepository(com.odysseusinc.arachne.portal.repository.SubmissionStatusHistoryRepository) BaseSubmissionService(com.odysseusinc.arachne.portal.service.submission.BaseSubmissionService) ContentStorageService(com.odysseusinc.arachne.storage.service.ContentStorageService) ArachneFileMeta(com.odysseusinc.arachne.storage.model.ArachneFileMeta) PostAuthorize(org.springframework.security.access.prepost.PostAuthorize) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) Objects(java.util.Objects) CommonFileUtils(com.odysseusinc.arachne.commons.utils.CommonFileUtils) SubmissionSpecification(com.odysseusinc.arachne.portal.model.search.SubmissionSpecification) List(java.util.List) ZipFile(net.lingala.zip4j.ZipFile) Optional(java.util.Optional) SubmissionActionType(com.odysseusinc.arachne.portal.service.impl.submission.SubmissionActionType) Queue(java.util.Queue) FilenameUtils(org.apache.commons.io.FilenameUtils) NOT_APPROVED(com.odysseusinc.arachne.portal.model.SubmissionStatus.NOT_APPROVED) EntityGraph(com.cosium.spring.data.jpa.entity.graph.domain.EntityGraph) DigestUtils(org.springframework.util.DigestUtils) LegacyAnalysisHelper(com.odysseusinc.arachne.portal.util.LegacyAnalysisHelper) Submission(com.odysseusinc.arachne.portal.model.Submission) SubmissionGroupSearch(com.odysseusinc.arachne.portal.model.search.SubmissionGroupSearch) ResultFileRepository(com.odysseusinc.arachne.portal.repository.ResultFileRepository) ValidationException(com.odysseusinc.arachne.portal.exception.ValidationException) EXECUTED_REJECTED(com.odysseusinc.arachne.portal.model.SubmissionStatus.EXECUTED_REJECTED) HashSet(java.util.HashSet) NoExecutableFileException(com.odysseusinc.arachne.portal.exception.NoExecutableFileException) EXECUTED(com.odysseusinc.arachne.portal.model.SubmissionStatus.EXECUTED) UserDetails(org.springframework.security.core.userdetails.UserDetails) LinkedList(java.util.LinkedList) UUIDGenerator(com.odysseusinc.arachne.commons.utils.UUIDGenerator) OutputStream(java.io.OutputStream) Logger(org.slf4j.Logger) SubmissionResultFileRepository(com.odysseusinc.arachne.portal.repository.SubmissionResultFileRepository) SequenceInputStream(java.io.SequenceInputStream) SubmissionAction(com.odysseusinc.arachne.portal.service.impl.submission.SubmissionAction) PermissionDeniedException(com.odysseusinc.arachne.portal.exception.PermissionDeniedException) WebSecurityConfig(com.odysseusinc.arachne.portal.config.WebSecurityConfig) NotExistException(com.odysseusinc.arachne.portal.exception.NotExistException) EntityUtils(com.odysseusinc.arachne.portal.util.EntityUtils) MultipartFile(org.springframework.web.multipart.MultipartFile) IN_PROGRESS(com.odysseusinc.arachne.portal.model.SubmissionStatus.IN_PROGRESS) SubmissionGroup(com.odysseusinc.arachne.portal.model.SubmissionGroup) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) InputStream(java.io.InputStream) Path(java.nio.file.Path) ZipFile(net.lingala.zip4j.ZipFile) ResultFile(com.odysseusinc.arachne.portal.model.ResultFile) File(java.io.File) AnalysisFile(com.odysseusinc.arachne.portal.model.AnalysisFile) SubmissionFile(com.odysseusinc.arachne.portal.model.SubmissionFile) ZipFile(net.lingala.zip4j.ZipFile) MultipartFile(org.springframework.web.multipart.MultipartFile) FileHeader(net.lingala.zip4j.model.FileHeader) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 7 with ZipFile

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;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Path(java.nio.file.Path) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) ArrayList(java.util.ArrayList) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) XMLValidAgainstXSDAssertionVerificationResult(io.irontest.models.assertion.XMLValidAgainstXSDAssertionVerificationResult) XMLValidAgainstXSDAssertionProperties(io.irontest.models.assertion.XMLValidAgainstXSDAssertionProperties) ZipFile(net.lingala.zip4j.ZipFile) ByteArrayInputStream(java.io.ByteArrayInputStream) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) File(java.io.File) ZipFile(net.lingala.zip4j.ZipFile) Validator(javax.xml.validation.Validator)

Example 8 with ZipFile

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());
}
Also used : ZipFile(net.lingala.zip4j.ZipFile) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ZipParameters(net.lingala.zip4j.model.ZipParameters)

Example 9 with ZipFile

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);
    }
}
Also used : ZipInputStream(net.lingala.zip4j.io.inputstream.ZipInputStream) ZipFile(net.lingala.zip4j.ZipFile) Descriptor(ru.woesss.j2me.jar.Descriptor) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileHeader(net.lingala.zip4j.model.FileHeader)

Example 10 with ZipFile

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();
            }
        }
    }
}
Also used : ZipFile(net.lingala.zip4j.ZipFile) HashMap(java.util.HashMap) InputStream(java.io.InputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) FileHeader(net.lingala.zip4j.model.FileHeader) IOException(java.io.IOException)

Aggregations

ZipFile (net.lingala.zip4j.ZipFile)12 FileHeader (net.lingala.zip4j.model.FileHeader)6 IOException (java.io.IOException)5 File (java.io.File)4 Path (java.nio.file.Path)3 ZipException (net.lingala.zip4j.exception.ZipException)3 IUser (com.odysseusinc.arachne.portal.model.IUser)2 Submission (com.odysseusinc.arachne.portal.model.Submission)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 ZipParameters (net.lingala.zip4j.model.ZipParameters)2 EntityGraph (com.cosium.spring.data.jpa.entity.graph.domain.EntityGraph)1 CommonFileUtils (com.odysseusinc.arachne.commons.utils.CommonFileUtils)1 UUIDGenerator (com.odysseusinc.arachne.commons.utils.UUIDGenerator)1 ApproveDTO (com.odysseusinc.arachne.portal.api.v1.dto.ApproveDTO)1 UpdateNotificationDTO (com.odysseusinc.arachne.portal.api.v1.dto.UpdateNotificationDTO)1 WebSecurityConfig (com.odysseusinc.arachne.portal.config.WebSecurityConfig)1 ConverterRuntimeException (com.odysseusinc.arachne.portal.exception.ConverterRuntimeException)1 NoExecutableFileException (com.odysseusinc.arachne.portal.exception.NoExecutableFileException)1 NotExistException (com.odysseusinc.arachne.portal.exception.NotExistException)1