Search in sources :

Example 41 with ZipFile

use of java.util.zip.ZipFile in project XobotOS by xamarin.

the class DexPathList method makeDexElements.

/**
     * Makes an array of dex/resource path elements, one per element of
     * the given array.
     */
private static Element[] makeDexElements(ArrayList<File> files, File optimizedDirectory) {
    ArrayList<Element> elements = new ArrayList<Element>();
    /*
         * Open all files and load the (direct or contained) dex files
         * up front.
         */
    for (File file : files) {
        ZipFile zip = null;
        DexFile dex = null;
        String name = file.getName();
        if (name.endsWith(DEX_SUFFIX)) {
            // Raw dex file (not inside a zip/jar).
            try {
                dex = loadDexFile(file, optimizedDirectory);
            } catch (IOException ex) {
                System.logE("Unable to load dex file: " + file, ex);
            }
        } else if (name.endsWith(APK_SUFFIX) || name.endsWith(JAR_SUFFIX) || name.endsWith(ZIP_SUFFIX)) {
            try {
                zip = new ZipFile(file);
            } catch (IOException ex) {
                /*
                     * Note: ZipException (a subclass of IOException)
                     * might get thrown by the ZipFile constructor
                     * (e.g. if the file isn't actually a zip/jar
                     * file).
                     */
                System.logE("Unable to open zip file: " + file, ex);
            }
            try {
                dex = loadDexFile(file, optimizedDirectory);
            } catch (IOException ignored) {
            /*
                     * IOException might get thrown "legitimately" by
                     * the DexFile constructor if the zip file turns
                     * out to be resource-only (that is, no
                     * classes.dex file in it). Safe to just ignore
                     * the exception here, and let dex == null.
                     */
            }
        } else {
            System.logW("Unknown file type for: " + file);
        }
        if ((zip != null) || (dex != null)) {
            elements.add(new Element(file, zip, dex));
        }
    }
    return elements.toArray(new Element[elements.size()]);
}
Also used : ZipFile(java.util.zip.ZipFile) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 42 with ZipFile

use of java.util.zip.ZipFile in project translationstudio8 by heartsome.

the class ImportProjectWizardPage2 method updateProjectsList.

public void updateProjectsList(final String path) {
    if (path == null || path.length() == 0) {
        setMessage(Messages.getString("wizard.ImportProjectWizardPage.desc"));
        selectedProjects = new ProjectRecord[0];
        setPageComplete(selectElementTree.getCheckedElements().length > 0);
        lastPath = path;
        return;
    }
    final File directory = new File(path);
    long modified = directory.lastModified();
    if (path.equals(lastPath) && lastModified == modified && lastCopyFiles == copyFiles) {
        // change, no refreshing is required
        return;
    }
    lastPath = path;
    lastModified = modified;
    lastCopyFiles = copyFiles;
    // We can't access the radio button from the inner class so get the
    // status beforehand
    final boolean dirSelected = false;
    try {
        getContainer().run(true, true, new IRunnableWithProgress() {

            @SuppressWarnings({ "rawtypes", "restriction" })
            public void run(IProgressMonitor monitor) {
                monitor.beginTask(Messages.getString("importProjectWizardPage.searchingMessage"), 100);
                selectedProjects = new ProjectRecord[0];
                Collection files = new ArrayList();
                monitor.worked(10);
                if (!dirSelected && ArchiveFileManipulations.isTarFile(path)) {
                    TarFile sourceTarFile = getSpecifiedTarSourceFile(path);
                    if (sourceTarFile == null) {
                        return;
                    }
                    structureProvider = new TarLeveledStructureProvider(sourceTarFile);
                    Object child = structureProvider.getRoot();
                    if (!collectProjectFilesFromProvider(files, child, 0, monitor)) {
                        return;
                    }
                    Iterator filesIterator = files.iterator();
                    selectedProjects = new ProjectRecord[files.size()];
                    int index = 0;
                    monitor.worked(50);
                    monitor.subTask(Messages.getString("importProjectWizardPage.processingMessage"));
                    while (filesIterator.hasNext()) {
                        selectedProjects[index++] = (ProjectRecord) filesIterator.next();
                    }
                } else if (!dirSelected && ArchiveFileManipulations.isZipFile(path)) {
                    ZipFile sourceFile = getSpecifiedZipSourceFile(path);
                    if (sourceFile == null) {
                        return;
                    }
                    structureProvider = new ZipLeveledStructureProvider(sourceFile);
                    Object child = structureProvider.getRoot();
                    if (!collectProjectFilesFromProvider(files, child, 0, monitor)) {
                        return;
                    }
                    Iterator filesIterator = files.iterator();
                    selectedProjects = new ProjectRecord[files.size()];
                    int index = 0;
                    monitor.worked(50);
                    monitor.subTask(Messages.getString("importProjectWizardPage.processingMessage"));
                    while (filesIterator.hasNext()) {
                        selectedProjects[index++] = (ProjectRecord) filesIterator.next();
                    }
                } else {
                    monitor.worked(60);
                }
                monitor.done();
            }
        });
    } catch (InvocationTargetException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (InterruptedException e) {
        LOGGER.error(e.getMessage(), e);
    }
    // 开始处理导入项目中的项目名称不合法的情况
    String projectName = "";
    StringBuffer errorProjectNameSB = new StringBuffer();
    StringBuffer errorCharSB = new StringBuffer();
    List<ProjectRecord> tempList = new ArrayList<ProjectRecord>();
    boolean isError = false;
    for (int i = 0; i < selectedProjects.length; i++) {
        projectName = selectedProjects[i].getProjectName();
        isError = false;
        for (int j = 0; j < Constant.RESOURCE_ERRORCHAR.length; j++) {
            if (projectName.indexOf(Constant.RESOURCE_ERRORCHAR[j]) != -1) {
                errorCharSB.append(Constant.RESOURCE_ERRORCHAR[j]);
                errorProjectNameSB.append(projectName + ", ");
                isError = true;
            }
        }
        if (!isError) {
            tempList.add(selectedProjects[i]);
        }
    }
    if (errorProjectNameSB.length() > 0) {
        final String errorTip = MessageFormat.format(Messages.getString("importProjectWizardPage.projectError"), new Object[] { errorProjectNameSB.toString().substring(0, errorProjectNameSB.toString().length() - 2), errorCharSB.toString() });
        Display.getDefault().asyncExec(new Runnable() {

            public void run() {
                MessageDialog.openWarning(getShell(), Messages.getString("importProject.all.dialog.warning"), errorTip);
            }
        });
    }
    selectedProjects = tempList.toArray(new ProjectRecord[tempList.size()]);
    setPageComplete(selectElementTree.getCheckedElements().length > 0);
    selectElementTree.refresh(true);
}
Also used : ZipLeveledStructureProvider(org.eclipse.ui.internal.wizards.datatransfer.ZipLeveledStructureProvider) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) TarLeveledStructureProvider(org.eclipse.ui.internal.wizards.datatransfer.TarLeveledStructureProvider) ZipFile(java.util.zip.ZipFile) TarFile(org.eclipse.ui.internal.wizards.datatransfer.TarFile) Iterator(java.util.Iterator) Collection(java.util.Collection) ZipFile(java.util.zip.ZipFile) TarFile(org.eclipse.ui.internal.wizards.datatransfer.TarFile) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 43 with ZipFile

use of java.util.zip.ZipFile in project translationstudio8 by heartsome.

the class ImportProjectWizardPage method updateProjectsList.

/**
	 * Update the list of projects based on path. Method declared public only
	 * for test suite.
	 * 
	 * @param path
	 */
public void updateProjectsList(final String path) {
    // on an empty path empty selectedProjects
    if (path == null || path.length() == 0) {
        setMessage(Messages.getString("wizard.ImportProjectWizardPage.desc"));
        selectedProjects = new ProjectRecord[0];
        projectsList.refresh(true);
        projectsList.setCheckedElements(selectedProjects);
        setPageComplete(projectsList.getCheckedElements().length > 0);
        lastPath = path;
        return;
    }
    final File directory = new File(path);
    long modified = directory.lastModified();
    if (path.equals(lastPath) && lastModified == modified && lastCopyFiles == copyFiles) {
        // change, no refreshing is required
        return;
    }
    lastPath = path;
    lastModified = modified;
    lastCopyFiles = copyFiles;
    // We can't access the radio button from the inner class so get the
    // status beforehand
    final boolean dirSelected = false;
    try {
        getContainer().run(true, true, new IRunnableWithProgress() {

            /*
				 * (non-Javadoc)
				 * 
				 * @see
				 * org.eclipse.jface.operation.IRunnableWithProgress#run(org
				 * .eclipse.core.runtime.IProgressMonitor)
				 */
            @SuppressWarnings("rawtypes")
            public void run(IProgressMonitor monitor) {
                monitor.beginTask(DataTransferMessages.WizardProjectsImportPage_SearchingMessage, 100);
                selectedProjects = new ProjectRecord[0];
                Collection files = new ArrayList();
                monitor.worked(10);
                if (!dirSelected && ArchiveFileManipulations.isTarFile(path)) {
                    TarFile sourceTarFile = getSpecifiedTarSourceFile(path);
                    if (sourceTarFile == null) {
                        return;
                    }
                    structureProvider = new TarLeveledStructureProvider(sourceTarFile);
                    Object child = structureProvider.getRoot();
                    if (!collectProjectFilesFromProvider(files, child, 0, monitor)) {
                        return;
                    }
                    Iterator filesIterator = files.iterator();
                    selectedProjects = new ProjectRecord[files.size()];
                    int index = 0;
                    monitor.worked(50);
                    monitor.subTask(DataTransferMessages.WizardProjectsImportPage_ProcessingMessage);
                    while (filesIterator.hasNext()) {
                        selectedProjects[index++] = (ProjectRecord) filesIterator.next();
                    }
                } else if (!dirSelected && ArchiveFileManipulations.isZipFile(path)) {
                    ZipFile sourceFile = getSpecifiedZipSourceFile(path);
                    if (sourceFile == null) {
                        return;
                    }
                    structureProvider = new ZipLeveledStructureProvider(sourceFile);
                    Object child = structureProvider.getRoot();
                    if (!collectProjectFilesFromProvider(files, child, 0, monitor)) {
                        return;
                    }
                    Iterator filesIterator = files.iterator();
                    selectedProjects = new ProjectRecord[files.size()];
                    int index = 0;
                    monitor.worked(50);
                    monitor.subTask(DataTransferMessages.WizardProjectsImportPage_ProcessingMessage);
                    while (filesIterator.hasNext()) {
                        selectedProjects[index++] = (ProjectRecord) filesIterator.next();
                    }
                } else {
                    monitor.worked(60);
                }
                monitor.done();
            }
        });
    } catch (InvocationTargetException e) {
        IDEWorkbenchPlugin.log(e.getMessage(), e);
    } catch (InterruptedException e) {
    // Nothing to do if the user interrupts.
    }
    projectsList.refresh(true);
    ProjectRecord[] projects = getProjectRecords();
    boolean displayWarning = false;
    for (int i = 0; i < projects.length; i++) {
        if (projects[i].hasConflicts) {
            displayWarning = true;
            projectsList.setGrayed(projects[i], true);
        } else {
            projectsList.setChecked(projects[i], true);
        }
    }
    if (displayWarning) {
        setMessage(DataTransferMessages.WizardProjectsImportPage_projectsInWorkspace, WARNING);
    } else {
        setMessage("");
    }
    setPageComplete(projectsList.getCheckedElements().length > 0);
    if (selectedProjects.length == 0) {
        setMessage(DataTransferMessages.WizardProjectsImportPage_noProjectsToImport, WARNING);
    }
}
Also used : ZipLeveledStructureProvider(org.eclipse.ui.internal.wizards.datatransfer.ZipLeveledStructureProvider) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) TarLeveledStructureProvider(org.eclipse.ui.internal.wizards.datatransfer.TarLeveledStructureProvider) ZipFile(java.util.zip.ZipFile) TarFile(org.eclipse.ui.internal.wizards.datatransfer.TarFile) Iterator(java.util.Iterator) Collection(java.util.Collection) ZipFile(java.util.zip.ZipFile) TarFile(org.eclipse.ui.internal.wizards.datatransfer.TarFile) File(java.io.File)

Example 44 with ZipFile

use of java.util.zip.ZipFile in project j2objc by google.

the class GenerationBatch method processJarFile.

private void processJarFile(String filename) {
    File f = findJarFile(filename);
    if (f == null) {
        ErrorUtil.error("No such file: " + filename);
        return;
    }
    // don't support Java-like source paths.
    if (options.emitLineDirectives()) {
        ErrorUtil.warning("source debugging of jar files is not supported: " + filename);
    }
    GenerationUnit combinedUnit = null;
    if (options.getHeaderMap().combineSourceJars()) {
        combinedUnit = GenerationUnit.newCombinedJarUnit(filename, options);
    }
    try {
        ZipFile zfile = new ZipFile(f);
        try {
            Enumeration<? extends ZipEntry> enumerator = zfile.entries();
            File tempDir = FileUtil.createTempDir(J2OBJC_TEMP_DIR_PREFIX);
            String tempDirPath = tempDir.getAbsolutePath();
            options.fileUtil().addTempDir(tempDirPath);
            options.fileUtil().appendSourcePath(tempDirPath);
            while (enumerator.hasMoreElements()) {
                ZipEntry entry = enumerator.nextElement();
                String internalPath = entry.getName();
                if (internalPath.endsWith(".java")) {
                    // Extract JAR file to a temporary directory
                    File outputFile = options.fileUtil().extractZipEntry(tempDir, zfile, entry);
                    InputFile newFile = new RegularInputFile(outputFile.getAbsolutePath(), internalPath);
                    if (combinedUnit != null) {
                        inputs.add(new ProcessingContext(newFile, combinedUnit));
                    } else {
                        addExtractedJarSource(newFile, filename, internalPath);
                    }
                }
            }
        } finally {
            // Also closes input stream.
            zfile.close();
        }
    } catch (ZipException e) {
        // Also catches JarExceptions
        logger.fine(e.getMessage());
        ErrorUtil.error("Error reading file " + filename + " as a zip or jar file.");
    } catch (IOException e) {
        ErrorUtil.error(e.getMessage());
    }
}
Also used : GenerationUnit(com.google.devtools.j2objc.gen.GenerationUnit) ZipFile(java.util.zip.ZipFile) RegularInputFile(com.google.devtools.j2objc.file.RegularInputFile) ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) InputFile(com.google.devtools.j2objc.file.InputFile) RegularInputFile(com.google.devtools.j2objc.file.RegularInputFile) File(java.io.File) ZipFile(java.util.zip.ZipFile) InputFile(com.google.devtools.j2objc.file.InputFile) RegularInputFile(com.google.devtools.j2objc.file.RegularInputFile)

Example 45 with ZipFile

use of java.util.zip.ZipFile in project jstorm by alibaba.

the class JStormUtils method extractDirFromJar.

/**
     * Extra dir from the jar to destdir
     *
     * @param jarpath
     * @param dir
     * @param destdir
     */
public static void extractDirFromJar(String jarpath, String dir, String destdir) {
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(jarpath);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries != null && entries.hasMoreElements()) {
            ZipEntry ze = entries.nextElement();
            if (!ze.isDirectory() && ze.getName().startsWith(dir)) {
                InputStream in = zipFile.getInputStream(ze);
                try {
                    File file = new File(destdir, ze.getName());
                    if (!file.getParentFile().mkdirs()) {
                        if (!file.getParentFile().isDirectory()) {
                            throw new IOException("Mkdirs failed to create " + file.getParentFile().toString());
                        }
                    }
                    OutputStream out = new FileOutputStream(file);
                    try {
                        byte[] buffer = new byte[8192];
                        int i;
                        while ((i = in.read(buffer)) != -1) {
                            out.write(buffer, 0, i);
                        }
                    } finally {
                        out.close();
                    }
                } finally {
                    if (in != null)
                        in.close();
                }
            }
        }
    } catch (Exception e) {
        LOG.warn("No " + dir + " from " + jarpath + "!\n" + e.getMessage());
    } finally {
        if (zipFile != null)
            try {
                zipFile.close();
            } catch (Exception e) {
                LOG.warn(e.getMessage());
            }
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) ZipFile(java.util.zip.ZipFile)

Aggregations

ZipFile (java.util.zip.ZipFile)637 ZipEntry (java.util.zip.ZipEntry)454 File (java.io.File)287 IOException (java.io.IOException)214 InputStream (java.io.InputStream)147 FileOutputStream (java.io.FileOutputStream)108 ZipOutputStream (java.util.zip.ZipOutputStream)92 Test (org.junit.Test)89 FileInputStream (java.io.FileInputStream)68 Enumeration (java.util.Enumeration)47 ArrayList (java.util.ArrayList)46 BufferedInputStream (java.io.BufferedInputStream)44 BufferedOutputStream (java.io.BufferedOutputStream)39 ZipInputStream (java.util.zip.ZipInputStream)35 ZipException (java.util.zip.ZipException)34 OutputStream (java.io.OutputStream)31 ClassReader (org.objectweb.asm.ClassReader)29 FileNotFoundException (java.io.FileNotFoundException)26 JarFile (java.util.jar.JarFile)26 ByteArrayOutputStream (java.io.ByteArrayOutputStream)24