Search in sources :

Example 51 with Artifact

use of se.light.assembly64.model.Artifact in project assembly64fx by freabemania.

the class UnzipService method extractZip.

public void extractZip(File source, File target, Artifact entry) throws Exception {
    byte[] buffer = new byte[1024];
    ZipInputStream zis = null;
    CancelableTask cancelTask = CancelableTask.of();
    int logCtr = 0;
    try {
        FileUtils.forceMkdir(target);
        zis = new ZipInputStream(new FileInputStream(source));
        ZipEntry ze = zis.getNextEntry();
        if (ze == null) {
            Analytics.sendEvent("invalid_zip", entry.getName());
            throw new ZipException("Invalid zipentry");
        }
        int ctr = 0;
        while (ze != null && cancelTask.isRunning()) {
            String fileName = ze.getName();
            File newFile = new File(target + File.separator + fileName);
            ctr++;
            try {
                if (ze.isDirectory()) {
                    FileUtils.forceMkdir(newFile);
                } else {
                    try {
                        FileUtils.forceMkdir(new File(newFile.getParent()));
                        FileOutputStream fos = new FileOutputStream(newFile);
                        int len;
                        while ((len = zis.read(buffer)) > 0 && cancelTask.isRunning()) {
                            fos.write(buffer, 0, len);
                        }
                        fos.close();
                        if (logCtr % 10 == 0) {
                            ProgressControlWrapper.getInstance().setProgressLabel("Installing " + newFile.getName());
                            double finished = (double) ((double) (((double) ctr / (double) entry.getEntries())));
                            ProgressControlWrapper.getInstance().increaseCurrentProgressStep(finished);
                            // lt 100k then give up
                            if (target.getFreeSpace() < 100000) {
                                GenericMessageDialogController.withErrorProps("Oopss...", "No space left on device!", true).showAndWait();
                                CancelTaskRegistry.getInstance().broadcastCancel();
                            }
                        }
                    } catch (Exception e) {
                        LOGGER.info("Unable to extract entry " + fileName);
                    }
                }
            } catch (Exception e) {
                LOGGER.error("Unable to extract " + ze.getName());
            }
            ze = zis.getNextEntry();
        }
        if (cancelTask.isCancelled()) {
            ProgressControlWrapper.getInstance().setProgressLabel("Cleaning out (may take a while), please wait...");
            FileUtils.forceDelete(LocalStorageUtil.getSubdirOrFile(target, entry.getFolder()));
        }
    } finally {
        try {
            zis.closeEntry();
        } catch (Exception e) {
            LOGGER.error("Unable to close zip");
        }
        try {
            zis.close();
        } catch (Exception e) {
            LOGGER.error("Unable to close zipstream");
        }
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) ZipException(java.util.zip.ZipException) File(java.io.File) FileInputStream(java.io.FileInputStream) ZipException(java.util.zip.ZipException) CancelableTask(se.light.assembly64.model.CancelableTask)

Aggregations

Artifact (org.apache.archiva.maven2.model.Artifact)37 Test (org.junit.Test)29 SearchService (org.apache.archiva.rest.api.services.SearchService)14 ArrayList (java.util.ArrayList)13 BrowseService (org.apache.archiva.rest.api.services.BrowseService)11 File (java.io.File)10 SearchRequest (org.apache.archiva.rest.api.model.SearchRequest)9 Artifact (se.light.assembly64.model.Artifact)9 ManagedRepositoriesService (org.apache.archiva.rest.api.services.ManagedRepositoriesService)8 RepositoriesService (org.apache.archiva.rest.api.services.RepositoriesService)8 Path (java.nio.file.Path)7 List (java.util.List)6 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)6 CancelableTask (se.light.assembly64.model.CancelableTask)6 IOException (java.io.IOException)5 Optional (java.util.Optional)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 ArtifactDelta (se.light.assembly64.model.ArtifactDelta)5 ArtifactType (se.light.assembly64.model.ArtifactType)5