📄 cmsimportfolder.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/db/CmsImportFolder.java,v $
* Date : $Date: 2006/03/27 14:52:24 $
* Version: $Revision: 1.33 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
*
* 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 Alkacon Software GmbH, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project 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 org.opencms.db;
import org.opencms.file.CmsFile;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsProperty;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.CmsVfsException;
import org.opencms.file.types.CmsResourceTypeFolder;
import org.opencms.main.CmsEvent;
import org.opencms.main.CmsException;
import org.opencms.main.I_CmsEventListener;
import org.opencms.main.OpenCms;
import org.opencms.util.CmsFileUtil;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* Allows to import resources from the filesystem or a ZIP file into the OpenCms VFS.<p>
*
* @author Alexander Kandzior
*
* @version $Revision: 1.33 $
*
* @since 6.0.0
*/
public class CmsImportFolder {
/** The OpenCms context object that provides the permissions. */
private CmsObject m_cms;
/** The name of the import folder to load resources from. */
private String m_importFolderName;
/** The import path in the OpenCms VFS. */
private String m_importPath;
/** The resource (folder or ZIP file) to import from in the real file system. */
private File m_importResource;
/** Will be true if the import resource is a valid ZIP file. */
private boolean m_validZipFile;
/** The import resource ZIP stream to load resources from. */
private ZipInputStream m_zipStreamIn;
/**
* Constructor for a new CmsImportFolder that will read from a ZIP file.<p>
*
* @param content the zip file to import
* @param importPath the path to the OpenCms VFS to import to
* @param cms a OpenCms context to provide the permissions
* @param noSubFolder if false no sub folder will be created
* @throws CmsException 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));
m_cms.readFolder(importPath, CmsResourceFilter.IGNORE_EXPIRATION);
// import the resources
importZipResource(m_zipStreamIn, m_importPath, noSubFolder);
} catch (Exception e) {
throw new CmsVfsException(Messages.get().container(Messages.ERR_IMPORT_FOLDER_1, importPath), e);
}
}
/**
* Constructor for a new CmsImportFolder that will read from the real file system.<p>
*
* @param importFolderName the folder to import
* @param importPath the path to the OpenCms VFS to import to
* @param cms a OpenCms context to provide the permissions
* @throws CmsException if something goes wrong
*/
public CmsImportFolder(String importFolderName, String importPath, CmsObject cms)
throws CmsException {
try {
m_importFolderName = importFolderName;
m_importPath = importPath;
m_cms = cms;
// open the import resource
getImportResource();
// first lock the destination path
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 e) {
throw new CmsVfsException(Messages.get().container(
Messages.ERR_IMPORT_FOLDER_2,
importFolderName,
importPath), e);
}
}
/**
* Returns true if a valid ZIP file was imported.<p>
*
* @return true if a valid ZIP file was imported
*/
public boolean isValidZipFile() {
return m_validZipFile;
}
/**
* Stores the import resource in an Object member variable.<p>
* @throws CmsVfsException if the file to import is no valid zipfile
*/
private void getImportResource() throws CmsVfsException {
// get the import resource
m_importResource = new File(m_importFolderName);
// check if this is a folder or a ZIP file
if (m_importResource.isFile()) {
try {
m_zipStreamIn = new ZipInputStream(new FileInputStream(m_importResource));
} catch (IOException e) {
// if file but no ZIP file throw an exception
throw new CmsVfsException(Messages.get().container(
Messages.ERR_NO_ZIPFILE_1,
m_importResource.getName()), e);
}
}
}
/**
* Imports the resources from the folder in the real file system to the OpenCms VFS.<p>
*
* @param folder the folder to import from
* @param importPath the OpenCms VFS import path to import to
* @throws Exception if something goes wrong during file IO
*/
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.createResource(importPath + currentFile.getName(), CmsResourceTypeFolder.RESOURCE_TYPE_ID);
importResources(currentFile, importPath + currentFile.getName() + "/");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -