Example usage for java.util.zip ZipInputStream closeEntry

List of usage examples for java.util.zip ZipInputStream closeEntry

Introduction

In this page you can find the example usage for java.util.zip ZipInputStream closeEntry.

Prototype

public void closeEntry() throws IOException 

Source Link

Document

Closes the current ZIP entry and positions the stream for reading the next entry.

Usage

From source file:edu.harvard.iq.dvn.core.web.study.AddFilesPage.java

private List<StudyFileEditBean> createStudyFilesFromZip(File uploadedInputFile) {
    List<StudyFileEditBean> fbList = new ArrayList<StudyFileEditBean>();

    // This is a Zip archive that we want to unpack, then upload/ingest individual
    // files separately

    ZipInputStream ziStream = null;
    ZipEntry zEntry = null;//w  w  w .  ja  v a 2  s.com
    FileOutputStream tempOutStream = null;

    try {
        // Create ingest directory for the study: 
        File dir = new File(uploadedInputFile.getParentFile(), study.getId().toString());
        if (!dir.exists()) {
            dir.mkdir();
        }

        // Open Zip stream: 

        ziStream = new ZipInputStream(new FileInputStream(uploadedInputFile));

        if (ziStream == null) {
            return null;
        }
        while ((zEntry = ziStream.getNextEntry()) != null) {
            // Note that some zip entries may be directories - we 
            // simply skip them:
            if (!zEntry.isDirectory()) {

                String fileEntryName = zEntry.getName();

                if (fileEntryName != null && !fileEntryName.equals("")) {

                    String dirName = null;
                    String finalFileName = null;

                    int ind = fileEntryName.lastIndexOf('/');

                    if (ind > -1) {
                        finalFileName = fileEntryName.substring(ind + 1);
                        if (ind > 0) {
                            dirName = fileEntryName.substring(0, ind);
                            dirName = dirName.replace('/', '-');
                        }
                    } else {
                        finalFileName = fileEntryName;
                    }

                    // http://superuser.com/questions/212896/is-there-any-way-to-prevent-a-mac-from-creating-dot-underscore-files
                    if (!finalFileName.startsWith("._")) {
                        File tempUploadedFile = FileUtil.createTempFile(dir, finalFileName);

                        tempOutStream = new FileOutputStream(tempUploadedFile);

                        byte[] dataBuffer = new byte[8192];
                        int i = 0;

                        while ((i = ziStream.read(dataBuffer)) > 0) {
                            tempOutStream.write(dataBuffer, 0, i);
                            tempOutStream.flush();
                        }

                        tempOutStream.close();

                        // We now have the unzipped file saved in the upload directory;

                        StudyFileEditBean tempFileBean = new StudyFileEditBean(tempUploadedFile,
                                studyService.generateFileSystemNameSequence(), study);
                        tempFileBean.setSizeFormatted(tempUploadedFile.length());

                        // And, if this file was in a legit (non-null) directory, 
                        // we'll use its name as the file category: 

                        if (dirName != null) {
                            tempFileBean.getFileMetadata().setCategory(dirName);
                        }

                        fbList.add(tempFileBean);
                    }
                }
            }
            ziStream.closeEntry();

        }

    } catch (Exception ex) {
        String msg = "Failed ot unpack Zip file/create individual study files";

        dbgLog.warning(msg);
        dbgLog.warning(ex.getMessage());

        //return null; 
    } finally {
        if (ziStream != null) {
            try {
                ziStream.close();
            } catch (Exception zEx) {
            }
        }
        if (tempOutStream != null) {
            try {
                tempOutStream.close();
            } catch (Exception ioEx) {
            }
        }
    }

    // should we delete uploadedInputFile before return?
    return fbList;
}

From source file:org.ejbca.ui.web.admin.rainterface.RAInterfaceBean.java

public String importProfilesFromZip(byte[] filebuffer) {
    if (log.isTraceEnabled()) {
        log.trace(">importProfiles(): " + importedProfileName + " - " + filebuffer.length + " bytes");
    }//from  w ww.  j  av  a  2 s . co m

    String retmsg = "";
    String faultXMLmsg = "";

    if (StringUtils.isEmpty(importedProfileName) || filebuffer.length == 0) {
        retmsg = "Error: No input file";
        log.error(retmsg);
        return retmsg;
    }

    int importedFiles = 0;
    int ignoredFiles = 0;
    int nrOfFiles = 0;
    try {
        ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(filebuffer));
        ZipEntry ze = zis.getNextEntry();
        if (ze == null) {
            retmsg = "Error: Expected a zip file. '" + importedProfileName + "' is not a  zip file.";
            log.error(retmsg);
            return retmsg;
        }

        do {
            nrOfFiles++;
            String filename = ze.getName();
            if (log.isDebugEnabled()) {
                log.debug("Importing file: " + filename);
            }

            if (ignoreFile(filename)) {
                ignoredFiles++;
                continue;
            }

            String profilename;
            filename = URLDecoder.decode(filename, "UTF-8");

            int index1 = filename.indexOf("_");
            int index2 = filename.lastIndexOf("-");
            int index3 = filename.lastIndexOf(".xml");
            profilename = filename.substring(index1 + 1, index2);
            int profileid = 0;
            try {
                profileid = Integer.parseInt(filename.substring(index2 + 1, index3));
            } catch (NumberFormatException e) {
                if (log.isDebugEnabled()) {
                    log.debug("NumberFormatException parsing certificate profile id: " + e.getMessage());
                }
                ignoredFiles++;
                continue;
            }
            if (log.isDebugEnabled()) {
                log.debug("Extracted profile name '" + profilename + "' and profile ID '" + profileid + "'");
            }

            if (ignoreProfile(filename, profilename, profileid)) {
                ignoredFiles++;
                continue;
            }

            if (endEntityProfileSession.getEndEntityProfile(profileid) != null) {
                int newprofileid = endEntityProfileSession.findFreeEndEntityProfileId();
                log.warn("Entity profileid '" + profileid + "' already exist in database. Using " + newprofileid
                        + " instead.");
                profileid = newprofileid;
            }

            byte[] filebytes = new byte[102400];
            int i = 0;
            while ((zis.available() == 1) && (i < filebytes.length)) {
                filebytes[i++] = (byte) zis.read();
            }

            EndEntityProfile eprofile = getEEProfileFromByteArray(profilename, filebytes);
            if (eprofile == null) {
                String msg = "Faulty XML file '" + filename + "'. Failed to read End Entity Profile.";
                log.info(msg + " Ignoring file.");
                ignoredFiles++;
                faultXMLmsg += filename + ", ";
                continue;
            }

            profiles.addEndEntityProfile(profilename, eprofile);
            importedFiles++;
            log.info("Added EndEntity profile: " + profilename);

        } while ((ze = zis.getNextEntry()) != null);
        zis.closeEntry();
        zis.close();
    } catch (UnsupportedEncodingException e) {
        retmsg = "Error: UTF-8 was not a known character encoding.";
        log.error(retmsg, e);
        return retmsg;
    } catch (IOException e) {
        log.error(e);
        retmsg = "Error: " + e.getLocalizedMessage();
        return retmsg;
    } catch (AuthorizationDeniedException e) {
        log.error(e);
        retmsg = "Error: " + e.getLocalizedMessage();
        return retmsg;
    } catch (EndEntityProfileExistsException e) {
        log.error(e);
        retmsg = "Error: " + e.getLocalizedMessage();
        return retmsg;
    }

    if (StringUtils.isNotEmpty(faultXMLmsg)) {
        faultXMLmsg = faultXMLmsg.substring(0, faultXMLmsg.length() - 2);
        retmsg = "Faulty XML files: " + faultXMLmsg + ". " + importedFiles + " profiles were imported.";
    } else {
        retmsg = importedProfileName + " contained " + nrOfFiles + " files. " + importedFiles
                + " EndEntity Profiles were imported and " + ignoredFiles + " files  were ignored.";
    }
    log.info(retmsg);

    return retmsg;
}

From source file:org.jahia.services.importexport.ImportExportBaseService.java

public void getFileList(Resource file, Map<String, Long> sizes, List<String> fileList) throws IOException {
    ZipInputStream zis = getZipInputStream(file);
    try {//from w  w  w . j ava  2s.  c  om
        while (true) {
            ZipEntry zipentry = zis.getNextEntry();
            if (zipentry == null)
                break;
            String name = zipentry.getName().replace('\\', '/');
            if (expandImportedFilesOnDisk) {
                final File file1 = new File(expandImportedFilesOnDiskPath + File.separator + name);
                if (zipentry.isDirectory()) {
                    file1.mkdirs();
                } else {
                    long timer = System.currentTimeMillis();
                    if (logger.isDebugEnabled()) {
                        logger.debug("Expanding {} into {}", zipentry.getName(), file1);
                    }
                    file1.getParentFile().mkdirs();
                    final OutputStream output = new BufferedOutputStream(new FileOutputStream(file1),
                            1024 * 64);
                    try {
                        IOUtils.copyLarge(zis, output);
                        if (logger.isDebugEnabled()) {
                            logger.debug("Expanded {} in {}", zipentry.getName(),
                                    DateUtils.formatDurationWords(System.currentTimeMillis() - timer));
                        }
                    } finally {
                        output.close();
                    }
                }
            }
            if (name.endsWith(".xml")) {
                BufferedReader br = new BufferedReader(new InputStreamReader(zis));
                try {
                    long i = 0;
                    while (br.readLine() != null) {
                        i++;
                    }
                    sizes.put(name, i);
                } finally {
                    IOUtils.closeQuietly(br);
                }
            } else {
                sizes.put(name, zipentry.getSize());
            }
            if (name.contains("/")) {
                fileList.add("/" + name);
            }
            zis.closeEntry();
        }
    } finally {
        closeInputStream(zis);
    }
}

From source file:org.exoplatform.faq.service.impl.JCRDataStorage.java

@Override
public boolean importData(String parentId, InputStream inputStream, boolean isZip) throws Exception {
    SessionProvider sProvider = CommonUtils.createSystemProvider();
    List<String> patchNodeImport = new ArrayList<String>();
    Node categoryNode = getFAQServiceHome(sProvider).getNode(parentId);
    Session session = categoryNode.getSession();
    NodeIterator iter = categoryNode.getNodes();
    while (iter.hasNext()) {
        patchNodeImport.add(iter.nextNode().getName());
    }//from   w w  w  .j  a  v a2s.com
    if (isZip) { // Import from zipfile
        ZipInputStream zipStream = new ZipInputStream(inputStream);
        while (zipStream.getNextEntry() != null) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            int available = -1;
            byte[] data = new byte[2048];
            while ((available = zipStream.read(data, 0, 1024)) > -1) {
                out.write(data, 0, available);
            }
            zipStream.closeEntry();
            out.close();
            InputStream input = new ByteArrayInputStream(out.toByteArray());
            session.importXML(categoryNode.getPath(), input, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
            session.save();
        }
        zipStream.close();
        calculateImportRootCategory(categoryNode);
    } else { // import from xml
        session.importXML(categoryNode.getPath(), inputStream, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
        session.save();
    }
    categoryNode = (Node) session.getItem(categoryNode.getPath());
    iter = categoryNode.getNodes();
    while (iter.hasNext()) {
        Node node = iter.nextNode();
        if (patchNodeImport.contains(node.getName()))
            patchNodeImport.remove(node.getName());
        else
            patchNodeImport.add(node.getName());
    }
    for (String string : patchNodeImport) {
        Node nodeParentQuestion = categoryNode.getNode(string);
        iter = getQuestionsIterator(nodeParentQuestion, EMPTY_STR, true);
        // Update number answers and regeister question node listener
        while (iter.hasNext()) {
            Node node = iter.nextNode();
            reUpdateNumberOfPublicAnswers(node);
        }
    }
    return true;
}

From source file:com.pianobakery.complsa.MainGui.java

public void unzip(String zipFilePath, String destDirectory) throws IOException {
    File destDir = new File(destDirectory);
    if (!destDir.exists()) {
        destDir.mkdir();/*from   ww w  .  ja  v  a2s .  c  o  m*/
    }
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
    ZipEntry entry = zipIn.getNextEntry();
    // iterates over entries in the zip file
    while (entry != null) {
        String filePath = destDirectory + File.separator + entry.getName();
        if (!entry.isDirectory()) {
            // if the entry is a file, extracts it
            extractFile(zipIn, filePath);
        } else {
            // if the entry is a directory, make the directory
            File dir = new File(filePath);
            dir.mkdir();
        }
        zipIn.closeEntry();
        entry = zipIn.getNextEntry();
    }
    zipIn.close();
}

From source file:org.crs4.entando.innomanager.aps.system.services.layer.LayerManager.java

@Override
public File unzipShapeFile(String layername, File zipFile, String zipFileName) {
    int page = 0;
    InputStream fis;//from w  w w .ja  va 2  s.  co  m
    ZipInputStream zis;
    FileOutputStream fos;
    byte[] buffer = new byte[1024];
    String type;
    File newFile;
    String path = "";
    WorkLayer layer;
    try {
        layer = this.getWorkLayer(layername);
        path = getShapeFileDiskFolder(layername);
        if (layer == null)
            return null;
    } catch (Throwable t) {
        ApsSystemUtils.logThrowable(t, this, "UnZipShapeFile");
        return null;
    }
    if (!zipFileName.substring(zipFileName.length() - 4).equals(".zip"))
        return null;
    String fileName = null;
    String shapefileName = null;
    //try to unzip 
    boolean reset = false;
    try {
        fis = FileUtils.openInputStream(zipFile);
        zis = new ZipInputStream(fis);
        //get the zipped file list entry
        ZipEntry ze = zis.getNextEntry();

        boolean[] ext = { true, true, true };
        // controllo contenuto zip ( only the first 3 files: .shx, .shp, .dbf with the same name)
        while (ze != null) {
            type = null;
            ApsSystemUtils.getLogger().info("parse file: " + ze.getName());
            if (ze.getName().substring(ze.getName().length() - 4).equals(".shp")
                    && (shapefileName == null
                            || ze.getName().substring(0, ze.getName().length() - 4).equals(shapefileName))
                    && ext[0]) {
                type = ".shp";
                ext[0] = false;
            } else if (ze.getName().substring(ze.getName().length() - 4).equals(".dbf")
                    && (shapefileName == null
                            || ze.getName().substring(0, ze.getName().length() - 4).equals(shapefileName))
                    && ext[1]) {
                type = ".dbf";
                ext[1] = false;
            } else if (ze.getName().substring(ze.getName().length() - 4).equals(".shx")
                    && (shapefileName == null
                            || ze.getName().substring(0, ze.getName().length() - 4).equals(shapefileName))
                    && ext[2]) {
                type = ".shx";
                ext[2] = false;
            }
            // else the shapefiles haven't good ext or name 
            ApsSystemUtils.getLogger().info("type: " + type);

            // set the correct name of the shapefiles  (the first valid)

            if (type != null) {
                if (fileName == null) {
                    shapefileName = ze.getName().substring(0, ze.getName().length() - 4);
                    fileName = zipFileName.substring(0, zipFileName.length() - 4);
                    boolean found = false;
                    /// if exist changename
                    while (!found) {
                        newFile = new File(path + fileName + ".shp");
                        if (newFile.exists())
                            fileName += "0";
                        else
                            found = true;
                    }
                }
                ApsSystemUtils.getLogger().info("file write: " + path + fileName + type);
                newFile = new File(path + fileName + type);
                if (newFile.exists())
                    FileUtils.forceDelete(newFile);
                newFile = new File(path + fileName + type);
                {
                    fos = new FileOutputStream(newFile);
                    int len;
                    while ((len = zis.read(buffer)) > 0) {
                        fos.write(buffer, 0, len);
                    }
                    fos.close();
                    zis.closeEntry();
                }
            }
            ze = zis.getNextEntry();
        }
        zis.close();
    } catch (Throwable t) {
        ApsSystemUtils.logThrowable(t, this, "UnZippingShapeFile");
        reset = true;
    }
    if (fileName != null) {
        if (reset) {
            removeShapeFile(layername, fileName + ".shp");
        } else {
            newFile = new File(path + fileName + ".shp");
            if (newFile.exists())
                return newFile;
        }
    }
    return null;
}

From source file:com.orange.mmp.midlet.MidletManager.java

/**
 * Get Midlet for download./*ww  w. ja  v a2 s .c  om*/
 * @param appId            The midlet main application ID
 * @param mobile          The mobile to use
 * @param isMidletSigned   Boolean indicating if the midlet is signed (true), unsigned (false), to sign (null)
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public ByteArrayOutputStream getJar(String appId, Mobile mobile, Boolean isMidletSigned) throws MMPException {
    if (appId == null)
        appId = ServiceManager.getInstance().getDefaultService().getId();

    //Search in Cache first
    String jarKey = appId + isMidletSigned + mobile.getKey();

    if (this.midletCache.isKeyInCache(jarKey)) {
        return (ByteArrayOutputStream) this.midletCache.get(jarKey).getValue();
    }

    Object extraCSSJadAttr = null;

    //Not found, build the JAR
    ByteArrayOutputStream output = null;
    ZipOutputStream zipOut = null;
    ZipInputStream zipIn = null;
    InputStream resourceStream = null;
    try {
        Midlet midlet = new Midlet();
        midlet.setType(mobile.getMidletType());
        Midlet[] midlets = (Midlet[]) DaoManagerFactory.getInstance().getDaoManager().getDao("midlet")
                .find(midlet);
        if (midlets.length == 0)
            throw new MMPException("Midlet type not found : " + mobile.getMidletType());
        else
            midlet = midlets[0];

        //Get navigation widget
        Widget appWidget = WidgetManager.getInstance().getWidget(appId, mobile.getBranchId());
        if (appWidget == null) {
            // Use Default if not found
            appWidget = WidgetManager.getInstance().getWidget(appId);
        }
        List<URL> embeddedResources = WidgetManager.getInstance().findWidgetResources("/m4m/", "*", appId,
                mobile.getBranchId(), false);
        output = new ByteArrayOutputStream();
        zipOut = new ZipOutputStream(output);
        zipIn = new ZipInputStream(new FileInputStream(new File(new URI(midlet.getJarLocation()))));

        ZipEntry entry;
        while ((entry = zipIn.getNextEntry()) != null) {
            zipOut.putNextEntry(entry);

            // Manifest found, modify it before delivery
            if (entry.getName().equals(Constants.JAR_MANIFEST_ENTRY) && appWidget != null) {
                Manifest midletManifest = new Manifest(zipIn);

                // TODO ? Remove optional permissions if midlet is not signed
                if (isMidletSigned != null && !isMidletSigned)
                    midletManifest.getMainAttributes().remove(Constants.JAD_PARAMETER_OPT_PERMISSIONS);

                midletManifest.getMainAttributes().putValue(Constants.JAD_PARAMETER_APPNAME,
                        appWidget.getName());
                String launcherLine = midletManifest.getMainAttributes()
                        .getValue(Constants.JAD_PARAMETER_LAUNCHER);
                Matcher launcherLineMatcher = launcherPattern.matcher(launcherLine);
                if (launcherLineMatcher.matches()) {
                    midletManifest.getMainAttributes().putValue(Constants.JAD_PARAMETER_LAUNCHER,
                            appWidget.getName().concat(", ").concat(launcherLineMatcher.group(2)).concat(", ")
                                    .concat(launcherLineMatcher.group(3)));
                } else
                    midletManifest.getMainAttributes().putValue(Constants.JAD_PARAMETER_LAUNCHER,
                            appWidget.getName());

                // Add/Modify/Delete MANIFEST parameters according to mobile rules
                JadAttributeAction[] jadActions = mobile.getJadAttributeActions();
                for (JadAttributeAction jadAction : jadActions) {
                    if (jadAction.getInManifest().equals(ApplyCase.ALWAYS)
                            || (isMidletSigned != null && isMidletSigned
                                    && jadAction.getInManifest().equals(ApplyCase.SIGNED))
                            || (isMidletSigned != null && !isMidletSigned
                                    && jadAction.getInManifest().equals(ApplyCase.UNSIGNED))) {
                        Attributes.Name attrName = new Attributes.Name(jadAction.getAttribute());
                        boolean exists = midletManifest.getMainAttributes().get(attrName) != null;
                        if (jadAction.isAddAction() || jadAction.isModifyAction()) {
                            if (exists || !jadAction.isStrict())
                                midletManifest.getMainAttributes().putValue(jadAction.getAttribute(),
                                        jadAction.getValue());
                        } else if (jadAction.isDeleteAction() && exists)
                            midletManifest.getMainAttributes().remove(attrName);
                    }
                }

                //Retrieve MeMo CSS extra attribute
                extraCSSJadAttr = midletManifest.getMainAttributes()
                        .get(new Attributes.Name(Constants.JAD_PARAMETER_MEMO_EXTRA_CSS));

                midletManifest.write(zipOut);
            }
            //Other files of Midlet
            else {
                IOUtils.copy(zipIn, zipOut);
            }
            zipIn.closeEntry();
            zipOut.closeEntry();
        }

        if (embeddedResources != null) {
            for (URL resourceUrl : embeddedResources) {
                resourceStream = resourceUrl.openConnection().getInputStream();
                String resourcePath = resourceUrl.getPath();
                entry = new ZipEntry(resourcePath.substring(resourcePath.lastIndexOf("/") + 1));
                entry.setTime(MIDLET_LAST_MODIFICATION_DATE);
                zipOut.putNextEntry(entry);
                IOUtils.copy(resourceStream, zipOut);
                zipOut.closeEntry();
                resourceStream.close();
            }
        }

        //Put JAR in cache for next uses
        this.midletCache.set(new Element(jarKey, output));

        //If necessary, add special CSS file if specified in JAD attributes
        if (extraCSSJadAttr != null) {
            String extraCSSSheetName = (String) extraCSSJadAttr;
            //Get resource stream
            resourceStream = WidgetManager.getInstance().getWidgetResource(
                    extraCSSSheetName + "/" + this.cssSheetsBundleName, mobile.getBranchId());

            if (resourceStream == null)
                throw new DataAccessResourceFailureException("no CSS sheet named " + extraCSSSheetName + " in "
                        + this.cssSheetsBundleName + " special bundle");

            //Append CSS sheet file into JAR
            entry = new ZipEntry(new File(extraCSSSheetName).getName());
            entry.setTime(MidletManager.MIDLET_LAST_MODIFICATION_DATE);
            zipOut.putNextEntry(entry);
            IOUtils.copy(resourceStream, zipOut);
            zipOut.closeEntry();
            resourceStream.close();
        }

        return output;

    } catch (IOException ioe) {
        throw new MMPException(ioe);
    } catch (URISyntaxException use) {
        throw new MMPException(use);
    } catch (DataAccessException dae) {
        throw new MMPException(dae);
    } finally {
        try {
            if (output != null)
                output.close();
            if (zipIn != null)
                zipIn.close();
            if (zipOut != null)
                zipOut.close();
            if (resourceStream != null)
                resourceStream.close();
        } catch (IOException ioe) {
            //NOP
        }
    }
}

From source file:org.eclipse.che.vfs.impl.fs.FSMountPoint.java

void unzip(VirtualFileImpl parent, InputStream zipped, boolean overwrite, int stripNumber)
        throws ForbiddenException, ConflictException, ServerException {
    if (!parent.isFolder()) {
        throw new ForbiddenException(
                String.format("Unable import zip content. Item '%s' is not a folder. ", parent.getPath()));
    }//from  w ww  . j  a  v  a2  s  .  c om
    final ZipContent zipContent;
    try {
        zipContent = ZipContent.newInstance(zipped);
    } catch (IOException e) {
        throw new ServerException(e.getMessage(), e);
    }
    if (!hasPermission(parent, BasicPermissions.WRITE.value(), true)) {
        throw new ForbiddenException(
                String.format("Unable import from zip to '%s'. Operation not permitted. ", parent.getPath()));
    }

    ZipInputStream zip = null;
    try {
        zip = new ZipInputStream(zipContent.zippedData);
        // Wrap zip stream to prevent close it. We can pass stream to other method and it can read content of current
        // ZipEntry but not able to close original stream of ZIPed data.
        InputStream noCloseZip = new NotClosableInputStream(zip);
        ZipEntry zipEntry;
        while ((zipEntry = zip.getNextEntry()) != null) {
            VirtualFileImpl current = parent;
            Path relPath = Path.fromString(zipEntry.getName());

            if (stripNumber > 0) {
                int currentLevel = relPath.elements().length;
                if (currentLevel <= stripNumber) {
                    continue;
                }
                relPath = relPath.subPath(stripNumber);
            }

            final String name = relPath.getName();
            if (relPath.length() > 1) {
                // create all required parent directories
                final Path parentPath = parent.getVirtualFilePath()
                        .newPath(relPath.subPath(0, relPath.length() - 1));
                current = new VirtualFileImpl(new java.io.File(ioRoot, toIoPath(parentPath)), parentPath,
                        pathToId(parentPath), this);
                if (!(current.exists() || current.getIoFile().mkdirs())) {
                    throw new ServerException(String.format("Unable create directory '%s' ", parentPath));
                }
            }
            final Path newPath = current.getVirtualFilePath().newPath(name);
            if (zipEntry.isDirectory()) {
                final java.io.File dir = new java.io.File(current.getIoFile(), name);
                if (!dir.exists()) {
                    if (dir.mkdir()) {
                        eventService.publish(new CreateEvent(workspaceId, newPath.toString(), true));
                    } else {
                        throw new ServerException(String.format("Unable create directory '%s' ", newPath));
                    }
                }
            } else {
                final VirtualFileImpl file = new VirtualFileImpl(new java.io.File(current.getIoFile(), name),
                        newPath, pathToId(newPath), this);
                if (file.exists()) {
                    if (isLocked(file)) {
                        throw new ForbiddenException(
                                String.format("File '%s' already exists and locked. ", file.getPath()));
                    }
                    if (!hasPermission(file, BasicPermissions.WRITE.value(), true)) {
                        throw new ForbiddenException(String
                                .format("Unable update file '%s'. Operation not permitted. ", file.getPath()));
                    }
                }

                boolean newFile;
                try {
                    if (!(newFile = file.getIoFile().createNewFile())) { // atomic
                        if (!overwrite) {
                            throw new ConflictException(
                                    String.format("File '%s' already exists. ", file.getPath()));
                        }
                    }
                } catch (IOException e) {
                    String msg = String.format("Unable create new file '%s'. ", newPath);
                    LOG.error(msg + e.getMessage(), e); // More details in log but do not show internal error to caller.
                    throw new ServerException(msg);
                }

                doUpdateContent(file, noCloseZip);
                if (newFile) {
                    eventService.publish(new CreateEvent(workspaceId, newPath.toString(), false));
                } else {
                    eventService.publish(new UpdateContentEvent(workspaceId, newPath.toString()));
                }
            }
            zip.closeEntry();
        }
        if (searcherProvider != null) {
            try {
                searcherProvider.getSearcher(this, true).add(parent);
            } catch (ServerException e) {
                LOG.error(e.getMessage(), e);
            }
        }
    } catch (IOException e) {
        throw new ServerException(e.getMessage(), e);
    } finally {
        closeQuietly(zip);
    }
}

From source file:org.openremote.modeler.cache.LocalFileCache.java

/**
 * Extracts a source resource archive to target path in local filesystem. Necessary
 * subdirectories will be created according to archive structure if necessary. Existing
 * files and directories matching the archive structure <b>will be deleted!</b>
 *
 * @param sourceArchive     file path to source archive in the local filesystem.
 * @param targetDirectory   file path to target directory where to extract the archive
 *
 * @throws CacheOperationException/* w  w w .  j  a va2  s.c  om*/
 *            if any file I/O errors occured during the extract operation
 *
 * @throws ConfigurationException
 *            if security manager has imposed access restrictions to the required files or
 *            directories
 */
private void extract(File sourceArchive, File targetDirectory)
        throws CacheOperationException, ConfigurationException {
    ZipInputStream archiveInput = null;
    ZipEntry zipEntry;

    try {
        archiveInput = new ZipInputStream(new BufferedInputStream(new FileInputStream(sourceArchive)));

        while ((zipEntry = archiveInput.getNextEntry()) != null) {
            if (zipEntry.isDirectory()) {
                // Do nothing -- relevant subdirectories will be created when handling the node files...

                continue;
            }

            File extractFile = new File(targetDirectory, zipEntry.getName());
            BufferedOutputStream extractOutput = null;

            try {
                FileUtilsExt.deleteQuietly(extractFile); // TODO : don't be quiet

                // create parent directories if necessary...

                if (!extractFile.getParentFile().exists()) {
                    boolean success = extractFile.getParentFile().mkdirs();

                    if (!success) {
                        throw new CacheOperationException(
                                "Unable to create cache folder directories ''{0}''. Reason unknown.",
                                extractFile.getParent());
                    }
                }

                extractOutput = new BufferedOutputStream(new FileOutputStream(extractFile));

                int len, bytecount = 0;
                byte[] buffer = new byte[4096];

                while ((len = archiveInput.read(buffer)) != -1) {
                    try {
                        extractOutput.write(buffer, 0, len);

                        bytecount += len;
                    }

                    catch (IOException e) {
                        throw new CacheOperationException("Error writing to ''{0}'' : {1}", e,
                                extractFile.getAbsolutePath(), e.getMessage());
                    }
                }

                cacheLog.debug("Wrote {0} bytes to ''{1}''...", bytecount, extractFile.getAbsolutePath());
            }

            catch (SecurityException e) {
                throw new ConfigurationException("Security manager has denied access to ''{0}'' : {1}", e,
                        extractFile.getAbsolutePath(), e.getMessage());
            }

            catch (FileNotFoundException e) {
                throw new CacheOperationException("Could not create file ''{0}'' : {1}", e,
                        extractFile.getAbsolutePath(), e.getMessage());
            }

            catch (IOException e) {
                throw new CacheOperationException("Error reading zip entry ''{0}'' from ''{1}'' : {2}", e,
                        zipEntry.getName(), sourceArchive.getAbsolutePath(), e.getMessage());
            }

            finally {
                if (extractOutput != null) {
                    try {
                        extractOutput.close();
                    }

                    catch (Throwable t) {
                        cacheLog.error("Could not close extracted file ''{0}'' : {1}", t,
                                extractFile.getAbsolutePath(), t.getMessage());
                    }
                }

                if (archiveInput != null) {
                    try {
                        archiveInput.closeEntry();
                    }

                    catch (Throwable t) {
                        cacheLog.warn("Could not close zip entry ''{0}'' in archive ''{1}'' : {2}", t,
                                zipEntry.getName(), t.getMessage());
                    }
                }
            }
        }
    }

    catch (SecurityException e) {
        throw new ConfigurationException("Security Manager has denied access to ''{0}'' : {1}", e,
                sourceArchive.getAbsolutePath(), e.getMessage());
    }

    catch (FileNotFoundException e) {
        throw new CacheOperationException("Archive ''{0}'' cannot be opened for reading : {1}", e,
                sourceArchive.getAbsolutePath(), e.getMessage());
    }

    catch (IOException e) {
        throw new CacheOperationException("Error reading archive ''{0}'' : {1}", e,
                sourceArchive.getAbsolutePath(), e.getMessage());
    }

    finally {
        try {
            if (archiveInput != null) {
                archiveInput.close();
            }
        }

        catch (Throwable t) {
            cacheLog.error("Error closing input stream to archive ''{0}'' : {1}", t,
                    sourceArchive.getAbsolutePath(), t.getMessage());
        }
    }
}

From source file:org.jahia.services.importexport.ImportExportBaseService.java

/**
 * Imports the content of the specified resource.
 *
 * @param parentNodePath the node to use as a parent for the import
 * @param file           the file with the content to be imported
 * @param rootBehaviour  the root behaviour (see {@link DocumentViewImportHandler})
 * @param session        current JCR session
 * @param filesToIgnore  set of files to be skipped
 * @throws IOException         in case of an I/O operation error
 * @throws RepositoryException in case of a JCR-related error
 *//*from w w  w  .  ja  v  a  2s . co  m*/
@Override
public void importZip(String parentNodePath, Resource file, int rootBehaviour, final JCRSessionWrapper session,
        Set<String> filesToIgnore, boolean useReferenceKeeper) throws IOException, RepositoryException {
    long timer = System.currentTimeMillis();
    if (filesToIgnore == null) {
        filesToIgnore = Collections.<String>emptySet();
    }
    logger.info("Start importing file {} into path {} ", file, parentNodePath != null ? parentNodePath : "/");

    Map<String, Long> sizes = new HashMap<String, Long>();
    List<String> fileList = new ArrayList<String>();

    Map<String, List<String>> references = new HashMap<String, List<String>>();

    getFileList(file, sizes, fileList);
    ZipInputStream zis;

    Map<String, String> pathMapping = session.getPathMapping();
    for (JahiaTemplatesPackage pkg : templatePackageRegistry.getRegisteredModules().values()) {
        String key = "/modules/" + pkg.getId() + "/";
        if (!pathMapping.containsKey(key)) {
            pathMapping.put(key, "/modules/" + pkg.getId() + "/" + pkg.getVersion() + "/");
        }
    }

    boolean importLive = sizes.containsKey(LIVE_REPOSITORY_XML);

    List<String> liveUuids = null;
    if (importLive) {
        // Import live content
        zis = getZipInputStream(file);
        try {
            while (true) {
                ZipEntry zipentry = zis.getNextEntry();
                if (zipentry == null)
                    break;
                String name = zipentry.getName();
                if (name.equals(LIVE_REPOSITORY_XML) && !filesToIgnore.contains(name)) {
                    long timerLive = System.currentTimeMillis();
                    logger.info("Start importing " + LIVE_REPOSITORY_XML);

                    final DocumentViewImportHandler documentViewImportHandler = new DocumentViewImportHandler(
                            session, parentNodePath, file, fileList);

                    documentViewImportHandler.setReferences(references);
                    documentViewImportHandler.setRootBehavior(rootBehaviour);
                    documentViewImportHandler.setBaseFilesPath("/live-content");
                    documentViewImportHandler.setAttributeProcessors(attributeProcessors);

                    Set<String> props = new HashSet<String>(documentViewImportHandler.getPropertiesToSkip());
                    props.remove(Constants.LASTPUBLISHED);
                    props.remove(Constants.LASTPUBLISHEDBY);
                    props.remove(Constants.PUBLISHED);
                    documentViewImportHandler.setPropertiesToSkip(props);
                    handleImport(zis, documentViewImportHandler, LIVE_REPOSITORY_XML);

                    logger.debug("Saving JCR session for " + LIVE_REPOSITORY_XML);

                    session.save(JCRObservationManager.IMPORT);

                    if (rootBehaviour == DocumentViewImportHandler.ROOT_BEHAVIOUR_RENAME) {
                        // Use path mapping to get new name
                        rootBehaviour = DocumentViewImportHandler.ROOT_BEHAVIOUR_REPLACE;
                    }

                    logger.debug("Resolving cross-references for " + LIVE_REPOSITORY_XML);

                    ReferencesHelper.resolveCrossReferences(session, references, useReferenceKeeper, true);

                    logger.debug("Saving JCR session for " + LIVE_REPOSITORY_XML);

                    session.save(JCRObservationManager.IMPORT);

                    liveUuids = documentViewImportHandler.getUuids();

                    logger.debug("Publishing...");

                    final JCRPublicationService publicationService = ServicesRegistry.getInstance()
                            .getJCRPublicationService();
                    final List<String> toPublish = documentViewImportHandler.getUuids();

                    JCRObservationManager.doWithOperationType(null, JCRObservationManager.IMPORT,
                            new JCRCallback<Object>() {
                                public Object doInJCR(JCRSessionWrapper session) throws RepositoryException {
                                    publicationService.publish(toPublish, Constants.EDIT_WORKSPACE,
                                            Constants.LIVE_WORKSPACE, false, false, null);
                                    return null;
                                }
                            });

                    logger.debug("publishing done");

                    String label = "published_at_" + new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss")
                            .format(GregorianCalendar.getInstance().getTime());
                    JCRVersionService.getInstance().addVersionLabel(toPublish, label, Constants.LIVE_WORKSPACE);

                    logger.info("Done importing " + LIVE_REPOSITORY_XML + " in {}",
                            DateUtils.formatDurationWords(System.currentTimeMillis() - timerLive));

                    break;
                }
                zis.closeEntry();

            }
        } catch (RepositoryException e) {
            throw e;
        } catch (Exception e) {
            logger.error("Cannot import", e);
        } finally {
            closeInputStream(zis);
        }
    }

    // Import repository content
    zis = getZipInputStream(file);
    try {
        while (true) {
            ZipEntry zipentry = zis.getNextEntry();
            if (zipentry == null)
                break;
            String name = zipentry.getName();
            if (name.equals(REPOSITORY_XML) && !filesToIgnore.contains(name)) {
                long timerDefault = System.currentTimeMillis();
                logger.info("Start importing " + REPOSITORY_XML);
                DocumentViewImportHandler documentViewImportHandler = new DocumentViewImportHandler(session,
                        parentNodePath, file, fileList);
                if (importLive) {
                    // Restore publication status
                    Set<String> props = new HashSet<String>(documentViewImportHandler.getPropertiesToSkip());
                    props.remove(Constants.LASTPUBLISHED);
                    props.remove(Constants.LASTPUBLISHEDBY);
                    props.remove(Constants.PUBLISHED);
                    documentViewImportHandler.setPropertiesToSkip(props);
                    documentViewImportHandler.setEnforceUuid(true);
                    documentViewImportHandler
                            .setUuidBehavior(DocumentViewImportHandler.IMPORT_UUID_COLLISION_MOVE_EXISTING);
                    documentViewImportHandler.setReplaceMultipleValues(true);
                    documentViewImportHandler.setRemoveMixins(true);
                }
                documentViewImportHandler.setReferences(references);
                documentViewImportHandler.setRootBehavior(rootBehaviour);
                documentViewImportHandler.setAttributeProcessors(attributeProcessors);
                handleImport(zis, documentViewImportHandler, REPOSITORY_XML);

                if (importLive && liveUuids != null) {
                    liveUuids.removeAll(documentViewImportHandler.getUuids());
                    Collections.reverse(liveUuids);
                    for (String uuid : liveUuids) {
                        // Uuids have been imported in live but not in default : need to be removed
                        try {
                            JCRNodeWrapper nodeToRemove = session.getNodeByIdentifier(uuid);
                            nodeToRemove.remove();
                        } catch (ItemNotFoundException | InvalidItemStateException ex) {
                            logger.debug("Node to remove has already been removed", ex);
                        }
                    }
                }
                logger.debug("Saving JCR session for " + REPOSITORY_XML);
                session.save(JCRObservationManager.IMPORT);
                logger.info("Done importing " + REPOSITORY_XML + " in {}",
                        DateUtils.formatDurationWords(System.currentTimeMillis() - timerDefault));
            } else if (name.endsWith(".xml") && !name.equals(REPOSITORY_XML)
                    && !name.equals(LIVE_REPOSITORY_XML) && !filesToIgnore.contains(name)
                    && !name.contains("/")) {
                long timerOther = System.currentTimeMillis();
                logger.info("Start importing {}", name);
                String thisPath = (parentNodePath != null
                        ? (parentNodePath + (parentNodePath.endsWith("/") ? "" : "/"))
                        : "") + StringUtils.substringBefore(name, ".xml");
                importXML(thisPath, zis, rootBehaviour, references, session);
                logger.info("Done importing {} in {}", name,
                        DateUtils.formatDurationWords(System.currentTimeMillis() - timerOther));
            }
            zis.closeEntry();

            // during import/export, never try to resolve the references between templates and site.
            RenderContext r;
            r = TemplateModuleInterceptor.renderContextThreadLocal.get();
            TemplateModuleInterceptor.renderContextThreadLocal.remove();

            ReferencesHelper.resolveCrossReferences(session, references, useReferenceKeeper);

            TemplateModuleInterceptor.renderContextThreadLocal.set(r);

            session.save(JCRObservationManager.IMPORT);
        }
    } catch (RepositoryException e) {
        throw e;
    } catch (Exception e) {
        logger.error("Cannot import", e);
    } finally {
        closeInputStream(zis);
    }

    if (importLive) {
        // Import user generated content
        zis = getZipInputStream(file);
        try {
            while (true) {
                ZipEntry zipentry = zis.getNextEntry();
                if (zipentry == null)
                    break;
                String name = zipentry.getName();
                if (name.equals(LIVE_REPOSITORY_XML)
                        && jcrStoreService.getSessionFactory().getCurrentUser() != null) {
                    long timerUGC = System.currentTimeMillis();
                    logger.info("Start importing user generated content");
                    JCRSessionWrapper liveSession = jcrStoreService.getSessionFactory()
                            .getCurrentUserSession("live", null, null);
                    DocumentViewImportHandler documentViewImportHandler = new DocumentViewImportHandler(
                            liveSession, parentNodePath, file, fileList);

                    documentViewImportHandler.setImportUserGeneratedContent(true);
                    documentViewImportHandler.setRootBehavior(rootBehaviour);
                    documentViewImportHandler.setBaseFilesPath("/live-content");
                    documentViewImportHandler.setAttributeProcessors(attributeProcessors);
                    liveSession.getPathMapping().putAll(pathMapping);
                    handleImport(zis, documentViewImportHandler, LIVE_REPOSITORY_XML);

                    logger.debug("Saving JCR session for UGC");

                    liveSession.save(JCRObservationManager.IMPORT);

                    // ReferencesHelper.resolveCrossReferences(liveSession, references);
                    // liveSession.save(JCRObservationManager.IMPORT);

                    logger.info("Done importing user generated content in {}",
                            DateUtils.formatDurationWords(System.currentTimeMillis() - timerUGC));
                    break;
                }
                zis.closeEntry();

            }
        } catch (Exception e) {
            logger.error("Cannot import", e);
        } finally {
            closeInputStream(zis);
        }
    }
    cleanFilesList(fileList);

    logger.info("Done importing file {} in {}", file,
            DateUtils.formatDurationWords(System.currentTimeMillis() - timer));
}