Search in sources :

Example 36 with ZipException

use of java.util.zip.ZipException in project yorc-a4c-plugin by ystia.

the class DeployTask method createZipEntries.

/**
 * Add all ZipEntry for this file path
 * If path is a directory, it must be ended by a "/".
 * All directory entries must be ended by a "/", and all simple file entries must be not.
 * TODO use this method everywhere
 * @param fullpath
 * @param zout
 */
private void createZipEntries(String fullpath, ZipOutputStream zout) throws IOException {
    log.debug("createZipEntries for " + fullpath);
    int index = 0;
    String name = "";
    while (name.length() < fullpath.length()) {
        index = fullpath.indexOf("/", index) + 1;
        if (index <= 1) {
            name = fullpath;
        } else {
            name = fullpath.substring(0, index);
        }
        try {
            zout.putNextEntry(new ZipEntry(name));
            log.debug("new ZipEntry: " + name);
        } catch (ZipException e) {
            if (e.getMessage().contains("duplicate")) {
            // log.debug("ZipEntry already added: " + name);
            } else {
                log.error("Cannot add ZipEntry: " + name, e);
                throw e;
            }
        }
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException)

Example 37 with ZipException

use of java.util.zip.ZipException in project incubator-weex by apache.

the class WXSoInstallMgrSdk method unZipSelectedFiles.

static boolean unZipSelectedFiles(String libName, int version, IWXUserTrackAdapter utAdapter) throws ZipException, IOException {
    String sourcePath = "lib/armeabi/lib" + libName + ".so";
    String zipPath = "";
    Context context = mContext;
    if (context == null) {
        return false;
    }
    ApplicationInfo aInfo = context.getApplicationInfo();
    if (null != aInfo) {
        zipPath = aInfo.sourceDir;
    }
    ZipFile zf;
    zf = new ZipFile(zipPath);
    try {
        for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements(); ) {
            ZipEntry entry = ((ZipEntry) entries.nextElement());
            if (entry.getName().startsWith(sourcePath)) {
                InputStream in = null;
                FileOutputStream os = null;
                FileChannel channel = null;
                int total = 0;
                try {
                    // Make sure the old library is deleted.
                    removeSoIfExit(libName, version);
                    // Copy file
                    in = zf.getInputStream(entry);
                    os = context.openFileOutput("lib" + libName + "bk" + version + ".so", Context.MODE_PRIVATE);
                    channel = os.getChannel();
                    byte[] buffers = new byte[1024];
                    int realLength;
                    while ((realLength = in.read(buffers)) > 0) {
                        // os.write(buffers);
                        channel.write(ByteBuffer.wrap(buffers, 0, realLength));
                        total += realLength;
                    }
                } finally {
                    if (in != null) {
                        try {
                            in.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    if (channel != null) {
                        try {
                            channel.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    if (os != null) {
                        try {
                            os.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    if (zf != null) {
                        zf.close();
                        zf = null;
                    }
                }
                if (total > 0) {
                    return _loadUnzipSo(libName, version, utAdapter);
                } else {
                    return false;
                }
            }
        }
    } catch (java.io.IOException e) {
        e.printStackTrace();
        WXExceptionUtils.commitCriticalExceptionRT(null, WXErrorCode.WX_KEY_EXCEPTION_SDK_INIT_CPU_NOT_SUPPORT, "unZipSelectedFiles", "[WX_KEY_EXCEPTION_SDK_INIT_unZipSelectedFiles] " + "\n Detail msg is: " + e.getMessage(), null);
    } finally {
        if (zf != null) {
            zf.close();
            zf = null;
        }
    }
    return false;
}
Also used : Context(android.content.Context) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileChannel(java.nio.channels.FileChannel) ZipEntry(java.util.zip.ZipEntry) ApplicationInfo(android.content.pm.ApplicationInfo) IOException(java.io.IOException) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) FileOutputStream(java.io.FileOutputStream)

Example 38 with ZipException

use of java.util.zip.ZipException in project grafikon by jub77.

the class LoadSave method load.

@Override
public TrainDiagram load(File file) throws LSException {
    try (ZipFile zip = new ZipFile(file)) {
        TrainDiagram diagram = null;
        // load metadata
        ZipEntry entry = zip.getEntry(METADATA);
        Properties metadata = new Properties();
        if (entry != null) {
            // load metadata
            metadata.load(zip.getInputStream(entry));
        }
        // set model version
        ModelVersion modelVersion = null;
        if (metadata.getProperty(METADATA_KEY_MODEL_VERSION) == null) {
            modelVersion = ModelVersion.parseModelVersion("1.0");
        } else {
            modelVersion = ModelVersion.parseModelVersion(metadata.getProperty(METADATA_KEY_MODEL_VERSION));
        }
        ModelVersion latest = LSSerializer.getLatestVersion();
        if (latest.getMajorVersion() < modelVersion.getMajorVersion() || (latest.getMajorVersion() == modelVersion.getMajorVersion() && latest.getMinorVersion() < modelVersion.getMinorVersion())) {
            throw new LSException("Cannot load newer model.");
        }
        // load train types
        entry = zip.getEntry(TRAIN_TYPES_NAME);
        InputStream isTypes = null;
        if (entry == null) {
            isTypes = DefaultTrainTypeListSource.getDefaultTypesInputStream();
        } else {
            isTypes = zip.getInputStream(entry);
        }
        LSTrainTypeSerializer tts = LSTrainTypeSerializer.getLSTrainTypeSerializer(modelVersion);
        LSTrainTypeList trainTypeList = tts.load(new InputStreamReader(isTypes, "utf-8"));
        // load model
        entry = zip.getEntry(TRAIN_DIAGRAM_NAME);
        if (entry == null) {
            throw new LSException("Model not found.");
        }
        diagram = this.loadTrainDiagram(modelVersion, metadata, new InputStreamReader(zip.getInputStream(entry), "utf-8"), trainTypeList);
        // load images
        LoadSaveImages lsImages = new LoadSaveImages();
        lsImages.loadTimetableImages(diagram, zip);
        return diagram;
    } catch (ZipException ex) {
        throw new LSException(ex);
    } catch (IOException ex) {
        throw new LSException(ex);
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException) Properties(java.util.Properties) TrainDiagram(net.parostroj.timetable.model.TrainDiagram) ZipFile(java.util.zip.ZipFile) ModelVersion(net.parostroj.timetable.model.ls.ModelVersion) LSException(net.parostroj.timetable.model.ls.LSException)

Example 39 with ZipException

use of java.util.zip.ZipException in project Bytecoder by mirkosertic.

the class ModulePath method readJar.

/**
 * Returns a {@code ModuleReference} to a module in modular JAR file on
 * the file system.
 *
 * @throws IOException
 * @throws FindException
 * @throws InvalidModuleDescriptorException
 */
private ModuleReference readJar(Path file) throws IOException {
    try (JarFile jf = new JarFile(file.toFile(), // verify
    true, ZipFile.OPEN_READ, releaseVersion)) {
        ModuleInfo.Attributes attrs;
        JarEntry entry = jf.getJarEntry(MODULE_INFO);
        if (entry == null) {
            // no module-info.class so treat it as automatic module
            try {
                ModuleDescriptor md = deriveModuleDescriptor(jf);
                attrs = new ModuleInfo.Attributes(md, null, null, null);
            } catch (RuntimeException e) {
                throw new FindException("Unable to derive module descriptor for " + jf.getName(), e);
            }
        } else {
            attrs = ModuleInfo.read(jf.getInputStream(entry), () -> jarPackages(jf));
        }
        return ModuleReferences.newJarModule(attrs, patcher, file);
    } catch (ZipException e) {
        throw new FindException("Error reading " + file, e);
    }
}
Also used : ModuleDescriptor(java.lang.module.ModuleDescriptor) FindException(java.lang.module.FindException) ZipException(java.util.zip.ZipException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 40 with ZipException

use of java.util.zip.ZipException in project ANNIS by korpling.

the class CorpusAdministration method importCorporaSave.

/**
 * Imports several corpora and catches a possible thrown
 * {@link DefaultAdministrationDao.ConflictingCorpusException} when the
 * overwrite flag is set to false.
 *
 * @param overwrite If set to false, a conflicting corpus is not silently
 * reimported.
 * @param aliasName An common alias name for all imported corpora or null
 * @param statusEmailAdress an email adress for informating the admin about
 * statuses
 * @param waitForOtherTasks If true wait for other imports to finish, if false
 * abort the import.
 * @param paths Valid pathes to corpora.
 * @return True if all corpora where imported successfully.
 */
public ImportStatus importCorporaSave(boolean overwrite, String aliasName, String statusEmailAdress, boolean waitForOtherTasks, List<String> paths) {
    // init the import stats. From the beginning everything is ok
    ImportStatus importStats = new ImportStatsImpl();
    importStats.setStatus(true);
    // check if database scheme is ok
    if (schemeFixer != null) {
        schemeFixer.checkAndFix();
    }
    List<File> roots = new LinkedList<>();
    for (String path : paths) {
        File f = new File(path);
        if (f.isFile()) {
            // might be a ZIP-file
            try (ZipFile zip = new ZipFile(f)) {
                // get the names of all corpora included in the ZIP file
                // in order to get a folder name
                Map<String, ZipEntry> corpora = ANNISFormatHelper.corporaInZipfile(zip);
                // unzip and add all resulting corpora to import list
                log.info("Unzipping " + f.getPath());
                File outDir = createZIPOutputDir(Joiner.on(", ").join(corpora.keySet()));
                roots.addAll(unzipCorpus(outDir, zip));
            } catch (ZipException ex) {
                log.error("" + f.getAbsolutePath() + " might not be a valid ZIP file and will be ignored", ex);
            } catch (IOException ex) {
                log.error("IOException when importing file " + f.getAbsolutePath() + ", will be ignored", ex);
            }
        } else {
            try {
                roots.addAll(ANNISFormatHelper.corporaInDirectory(f).values());
            } catch (IOException ex) {
                log.error("Could not find any corpus in " + f.getPath(), ex);
                importStats.setStatus(false);
                importStats.addException(f.getAbsolutePath(), ex);
            }
        }
    }
    // end for each given path
    // import each corpus separately
    boolean anyCorpusImported = false;
    for (File r : roots) {
        try {
            log.info("Importing corpus from: " + r.getPath());
            if (administrationDao.importCorpus(r.getPath(), aliasName, overwrite, waitForOtherTasks)) {
                log.info("Finished import from: " + r.getPath());
                sendImportStatusMail(statusEmailAdress, r.getPath(), ImportJob.Status.SUCCESS, null);
                anyCorpusImported = true;
            } else {
                importStats.setStatus(false);
                sendImportStatusMail(statusEmailAdress, r.getPath(), ImportJob.Status.ERROR, null);
            }
        } catch (AdministrationDao.ConflictingCorpusException ex) {
            importStats.setStatus(false);
            importStats.addException(r.getPath(), ex);
            log.error("Error on conflicting top level corpus name for {}", r.getPath());
            sendImportStatusMail(statusEmailAdress, r.getPath(), ImportJob.Status.ERROR, ex.getMessage());
        } catch (org.springframework.transaction.CannotCreateTransactionException ex) {
            importStats.setStatus(false);
            importStats.addException(r.getPath(), ex);
            log.error("Postgres is not running or misconfigured");
        } catch (Throwable ex) {
            importStats.setStatus(false);
            importStats.addException(r.getPath(), ex);
            log.error("Error on importing corpus", ex);
            sendImportStatusMail(statusEmailAdress, r.getPath(), ImportJob.Status.ERROR, ex.getMessage());
        }
    }
    return importStats;
}
Also used : ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) LinkedList(java.util.LinkedList) ZipFile(java.util.zip.ZipFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Aggregations

ZipException (java.util.zip.ZipException)188 IOException (java.io.IOException)89 File (java.io.File)71 ZipEntry (java.util.zip.ZipEntry)66 ZipFile (java.util.zip.ZipFile)62 InputStream (java.io.InputStream)45 FileInputStream (java.io.FileInputStream)37 ZipInputStream (java.util.zip.ZipInputStream)26 BufferedInputStream (java.io.BufferedInputStream)22 FileOutputStream (java.io.FileOutputStream)21 JarFile (java.util.jar.JarFile)21 JarEntry (java.util.jar.JarEntry)19 FileNotFoundException (java.io.FileNotFoundException)18 ArrayList (java.util.ArrayList)17 ZipOutputStream (java.util.zip.ZipOutputStream)15 URL (java.net.URL)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 GZIPInputStream (java.util.zip.GZIPInputStream)10 BufferedOutputStream (java.io.BufferedOutputStream)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7