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

📄 cmsimportfolder.java

📁 java 编写的程序
💻 JAVA
字号:
/*
* File   : $Source: /usr/local/cvs/opencms/src/com/opencms/file/CmsImportFolder.java,v $
* Date   : $Date: 2001/07/31 15:50:13 $
* Version: $Revision: 1.9 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001  The OpenCms Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* For further information about OpenCms, please see the
* OpenCms Website: http://www.opencms.org 
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package com.opencms.file;

import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.lang.reflect.*;
import com.opencms.core.*;
import com.opencms.template.*;
import org.w3c.dom.*;
import com.opencms.util.*;

/**
 * This class holds the functionaility to import resources from the filesystem
 * into the cms.
 *
 * @author Andreas Schouten
 * @version $Revision: 1.9 $ $Date: 2001/07/31 15:50:13 $
 */
public class CmsImportFolder implements I_CmsConstants {

    /**
     * The import-file to load resources from
     */
    private String m_importFile;

    /**
     * The import-resource (zip) to load resources from
     */
    private ZipInputStream m_zipStreamIn = null;

    /**
     *
     */
    private boolean m_validZipFile = false;

    /**
     * The import-path to write resources into the cms.
     */
    private String m_importPath;

    /**
     * The cms-object to do the operations.
     */
    private CmsObject m_cms;

    /**
     * The folder-object to import from.
     */
    private File m_importResource;

    /**
     * This constructs a new CmsImport-object which imports the resources.
     *
     * @param importFile the file or folder to import from.
     * @param importPath the path to the cms to import into.
     * @exception CmsException the CmsException is thrown if something goes wrong.
     */
    public CmsImportFolder(String importFile, String importPath, CmsObject cms)
        throws CmsException {

        try {
            m_importFile = importFile;
            m_importPath = importPath;
            m_cms = cms;

            // open the import resource
            getImportResource();

            // frist lock the path to import into.
            m_cms.lockResource(m_importPath);

            // import the resources
            if( m_zipStreamIn == null) {
                importResources(m_importResource, m_importPath);
            } else {
                importZipResource(m_zipStreamIn, m_importPath, false);
            }

            // all is done, unlock the resources
            m_cms.unlockResource(m_importPath);

        } catch( Exception exc ) {
            throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
        }
    }
    /**
     * This constructs a new CmsImport-object which imports the resources.
     *
     * @param content the zip file to import
     * @param importPath the path to the cms to import into.
     * @exception CmsException the CmsException is thrown if something goes wrong.
     */
    public CmsImportFolder(byte[] content, String importPath, CmsObject cms,
                            boolean noSubFolder) throws CmsException {

        m_importPath = importPath;
        m_cms = cms;

        try {
            // open the import resource
            m_zipStreamIn = new ZipInputStream(new ByteArrayInputStream(
                                                        content) );

            CmsFolder impFold = m_cms.readFolder(importPath);

            /*
            if( !impFold.isLocked() && !importPath.equals("/") ) {
                // frist lock the path to import into.
                m_cms.lockResource(m_importPath);
            }*/

            // check if user is allowed to write to the folder ...
            /*
            if( m_cms.accessWrite(importPath) == false) {
                throw new CmsException(CmsException.C_NO_ACCESS);
            }*/

            // import the resources
            importZipResource(m_zipStreamIn, m_importPath, noSubFolder);

            // all is done, unlock the resources
            /*
            if( impFold.isLocked() && !importPath.equals("/") ) {
                m_cms.unlockResource(m_importPath);
            }*/

        } catch( Exception exc ) {
            throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
        }
    }


    /**
     * Returns a byte-array containing the content of the file.
     *
     * @param filename The name of the file to read.
     * @return bytes[] The content of the file.
     */
    private byte[] getFileBytes(File file)
        throws Exception{
        FileInputStream fileStream = new FileInputStream(file);
        int charsRead = 0;
        int size = new Long(file.length()).intValue();
        byte[] buffer = new byte[size];
        while(charsRead < size) {
            charsRead += fileStream.read(buffer, charsRead, size - charsRead);
        }
        fileStream.close();
        return buffer;
    }

    /**
     * Gets the file-type for the filename.
     */
    private String getFileType(String filename)
        throws CmsException {
        String suffix = filename.substring(filename.lastIndexOf('.')+1);
        suffix = suffix.toLowerCase(); // file extension of filename

        // read the known file extensions from the database
        Hashtable extensions = m_cms.readFileExtensions();
        String resType = new String();
        if (extensions != null) {
            resType = (String) extensions.get(suffix);
        }
        if (resType == null) {
            resType = "plain";
        }
        return resType;
    }

    /**
     * Gets the import resource and stores it in object-member.
     */
    private void getImportResource() throws CmsException {
        try {
            // get the import resource
            m_importResource = new File(m_importFile);

            // if it is a file throw exception.
            if(m_importResource.isFile()) {
                try{
                    m_zipStreamIn = new ZipInputStream(new FileInputStream(
                                                        m_importResource) );
                } catch(IOException e) {
                    throw new CmsException("Exception: " + e.toString() );
                }
            }
        } catch(Exception exc) {
            throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
        }
    }
    /**
     * Imports the resources from the folder to the importPath.
     *
     * @param folder the file-object to import from.
     * @param importPath the import-path to import into.
     */
    private void importResources(File folder, String importPath)
        throws Exception {
        String[] diskFiles = folder.list();
        File currentFile;

        for(int i = 0; i < diskFiles.length; i++) {
            currentFile = new File(folder, diskFiles[i]);

            if(currentFile.isDirectory()) {
                // create directory in cms
                m_cms.createFolder(importPath, currentFile.getName());
                importResources(currentFile, importPath + currentFile.getName() + "/");
            } else {
                // import file into cms
                String type = getFileType( currentFile.getName() );
                byte[] content = getFileBytes(currentFile);
                // create the file
                m_cms.createFile(importPath, currentFile.getName(), content, type);
            }
        }
    }

    /**
     * imports the zip File to the import path
     *
     * @param zipStreamIn the input Stream
     * @param importPath the path in the vfs
     * @param noSubFolder create subFolders or not
     */
     private void importZipResource(ZipInputStream zipStreamIn,
                                    String importPath, boolean noSubFolder)
            throws Exception {
        boolean isFolder = false;
        boolean exit = false;
        int j, r, stop, charsRead, size;
        int entries = 0;
        int totalBytes = 0;
        int offset = 0;
        CmsFile file = null;
        byte[] buffer = null;
        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;
            // separete path in direcotries an file name ...
            StringTokenizer st = new StringTokenizer(entry.getName(), "/\\");
            int count = st.countTokens();
            String[] path = new String[count];

            if(entry.getName().endsWith("\\") || entry.getName().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==true ? path.length : path.length-1;

            if(noSubFolder == true) {stop = 0;}
            // now write the folders ...
            for(r=0; r < stop; r++) {
                try {
                    m_cms.createFolder(actImportPath, path[r] );
                } catch(CmsException e) {
                    // of course some folders did already exist!
                }
                actImportPath += path[r] += "/";
            }
            if(isFolder == false) {
                // import file into cms
                String type = getFileType( path[path.length-1] );

                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 == true) {
                            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(); }
                }
                file = null; // reset file
                // create the file
                try { // check if file exists ...
                    //file = m_cms.readFile(actImportPath, path[path.length-1]);
                    m_cms.lockResource(actImportPath + path[path.length-1], true);
                    m_cms.deleteFile(actImportPath + path[path.length-1]);
                } catch(CmsException e) {
                    // ignore the exception (did not exist)
                }
                try {
                    // new file ...
                    m_cms.createFile(actImportPath, path[path.length-1],
                                                            buffer, type);
                } catch(CmsException e) {
                    // ignore the exception
                    throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, e);
                }
            }
            // close the entry ...
            zipStreamIn.closeEntry();
        }
        zipStreamIn.close();
        if(entries > 0){
            // at least one entry, got a valid zip file ...
            setValidZipFile(true);
        }
     }

    /**
     * return true if we got a valid zip file
     */
     public boolean isValidZipFile() {
        return m_validZipFile;
     }

    /**
     * set valid or invalid
     */
     private void setValidZipFile(boolean valid) {
        m_validZipFile = valid;
     }
}

⌨️ 快捷键说明

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