⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmsimportfolder.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            } else {
                // import file into cms
                int type = OpenCms.getResourceManager().getDefaultTypeForName(currentFile.getName()).getTypeId();
                byte[] content = CmsFileUtil.readFile(currentFile);
                // create the file
                m_cms.createResource(importPath + currentFile.getName(), type, content, null);
                content = null;
            }
        }
    }

    /**
     * Imports the resources from a ZIP file in the real file system to the OpenCms VFS.<p>
     *
     * @param zipStreamIn the input Stream
     * @param importPath the path in the vfs
     * @param noSubFolder create subFolders or not
     * @throws Exception if something goes wrong during file IO 
     */
    private void importZipResource(ZipInputStream zipStreamIn, String importPath, boolean noSubFolder) throws Exception {

        int todo = 0;
        // TODO: this method looks very crude, it should be re-written sometime...
        
        boolean isFolder = false;
        boolean exit = false;
        int j, r, stop, charsRead, size;
        int entries = 0;
        int totalBytes = 0;
        int offset = 0;
        byte[] buffer = null;
        boolean resourceExists;

        while (true) {
            // handle the single entries ...
            j = 0;
            stop = 0;
            charsRead = 0;
            totalBytes = 0;
            // open the entry ...
            ZipEntry entry = zipStreamIn.getNextEntry();
            if (entry == null) {
                break;
            }
            entries++; // count number of entries in zip
            String actImportPath = importPath;
            String title = CmsResource.getName(entry.getName());
            String filename = m_cms.getRequestContext().getFileTranslator().translateResource(entry.getName());
            // separate path in direcotries an file name ...
            StringTokenizer st = new StringTokenizer(filename, "/\\");
            int count = st.countTokens();
            String[] path = new String[count];

            if (filename.endsWith("\\") || filename.endsWith("/")) {
                isFolder = true; // last entry is a folder
            } else {
                isFolder = false; // last entry is a file
            }
            while (st.hasMoreTokens()) {
                // store the files and folder names in array ...
                path[j] = st.nextToken();
                j++;
            }
            stop = isFolder ? path.length : (path.length - 1);

            if (noSubFolder) {
                stop = 0;
            }
            // now write the folders ...
            for (r = 0; r < stop; r++) {
                try {
                    m_cms.createResource(actImportPath + path[r], CmsResourceTypeFolder.RESOURCE_TYPE_ID);
                } catch (CmsException e) {
                    // of course some folders did already exist!
                }
                actImportPath += path[r];
                actImportPath += "/";
            }
            if (!isFolder) {                               
                // import file into cms
                int type = OpenCms.getResourceManager().getDefaultTypeForName(path[path.length - 1]).getTypeId();
                size = new Long(entry.getSize()).intValue();
                if (size == -1) {
                    Vector v = new Vector();
                    while (true) {
                        buffer = new byte[512];
                        offset = 0;
                        while (offset < buffer.length) {
                            charsRead = zipStreamIn.read(buffer, offset, buffer.length - offset);
                            if (charsRead == -1) {
                                exit = true;
                                break; // end of stream
                            }
                            offset += charsRead;
                            totalBytes += charsRead;
                        }
                        if (offset > 0) {
                            v.addElement(buffer);
                        }
                        if (exit) {
                            exit = false;
                            break;
                        }
                    }
                    buffer = new byte[totalBytes];
                    offset = 0;
                    byte[] act = null;
                    for (int z = 0; z < v.size() - 1; z++) {
                        act = (byte[])v.elementAt(z);
                        System.arraycopy(act, 0, buffer, offset, act.length);
                        offset += act.length;
                    }
                    act = (byte[])v.lastElement();
                    if ((totalBytes > act.length) && (totalBytes % act.length != 0)) {
                        totalBytes = totalBytes % act.length;
                    } else if ((totalBytes > act.length) && (totalBytes % act.length == 0)) {
                        totalBytes = act.length;
                    }
                    System.arraycopy(act, 0, buffer, offset, totalBytes);
                    // handle empty files ...
                    if (totalBytes == 0) {
                        buffer = " ".getBytes();
                    }
                } else {
                    // size was read clearly ...
                    buffer = new byte[size];
                    while (charsRead < size) {
                        charsRead += zipStreamIn.read(buffer, charsRead, size - charsRead);
                    }
                    // handle empty files ...
                    if (size == 0) {
                        buffer = " ".getBytes();
                    }
                }

                filename = actImportPath + path[path.length - 1];

                try {
                    m_cms.lockResource(filename);

                    m_cms.readResource(filename);
                    resourceExists = true;
                } catch (CmsException e) {
                    resourceExists = false;
                }

                if (resourceExists) {
                    CmsResource res = m_cms.readResource(filename, CmsResourceFilter.ALL);
                    CmsFile file = CmsFile.upgrade(res, m_cms);
                    byte[] contents = file.getContents();
                    try {
                        m_cms.replaceResource(filename, res.getTypeId(), buffer, Collections.EMPTY_LIST);
                    } catch (CmsDbSqlException sqlExc) {
                        // SQL error, probably the file is too large for the database settings, restore content
                        file.setContents(contents);
                        m_cms.writeFile(file);
                        throw sqlExc;
                    }

                    OpenCms.fireCmsEvent(new CmsEvent(
                        I_CmsEventListener.EVENT_RESOURCE_AND_PROPERTIES_MODIFIED,
                        Collections.singletonMap("resource", res)));
                } else {
                    String newResName = actImportPath + path[path.length - 1];
                    if (title.lastIndexOf('.') != -1) {
                        title = title.substring(0, title.lastIndexOf('.'));
                    }
                    List properties = new ArrayList(1);
                    CmsProperty titleProp = new CmsProperty();
                    titleProp.setName(CmsPropertyDefinition.PROPERTY_TITLE);
                    if (OpenCms.getWorkplaceManager().isDefaultPropertiesOnStructure()) {
                        titleProp.setStructureValue(title);
                    } else {
                        titleProp.setResourceValue(title);
                    }
                    properties.add(titleProp);
                    try {
                        m_cms.createResource(newResName, type, buffer, properties);
                    } catch (CmsDbSqlException sqlExc) {
                        // SQL error, probably the file is too large for the database settings, delete file
                        m_cms.lockResource(newResName);
                        m_cms.deleteResource(newResName, CmsResource.DELETE_PRESERVE_SIBLINGS);
                        throw sqlExc;
                    }
                }
            }

            // close the entry ...
            zipStreamIn.closeEntry();
        }
        zipStreamIn.close();
        if (entries > 0) {
            // at least one entry, got a valid zip file ...
            m_validZipFile = true;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -