/********************************************************* begin of preamble
**
** Copyright (C) 2003-2010 Software- und Organisations-Service GmbH.
** All rights reserved.
**
** This file may be used under the terms of either the
**
** GNU General Public License version 2.0 (GPL)
**
** as published by the Free Software Foundation
** http://www.gnu.org/licenses/gpl-2.0.txt and appearing in the file
** LICENSE.GPL included in the packaging of this file.
**
** or the
**
** Agreement for Purchase and Licensing
**
** as offered by Software- und Organisations-Service GmbH
** in the respective terms of supply that ship with this file.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
** IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
** THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
** BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
** POSSIBILITY OF SUCH DAMAGE.
********************************************************** end of preamble*/
package com.sos.VirtualFileSystem.zip;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import org.apache.log4j.Logger;
import com.sos.JSHelper.Basics.JSJobUtilities;
import com.sos.JSHelper.Exceptions.JobSchedulerException;
import com.sos.JSHelper.Options.SOSOptionTransferMode;
import com.sos.JSHelper.io.Files.JSFile;
import com.sos.VirtualFileSystem.DataElements.SOSFileList;
import com.sos.VirtualFileSystem.DataElements.SOSFolderName;
import com.sos.VirtualFileSystem.Interfaces.ISOSAuthenticationOptions;
import com.sos.VirtualFileSystem.Interfaces.ISOSConnection;
import com.sos.VirtualFileSystem.Interfaces.ISOSConnectionOptions;
import com.sos.VirtualFileSystem.Interfaces.ISOSSession;
import com.sos.VirtualFileSystem.Interfaces.ISOSShellOptions;
import com.sos.VirtualFileSystem.Interfaces.ISOSVFSHandler;
import com.sos.VirtualFileSystem.Interfaces.ISOSVfsFileTransfer;
import com.sos.VirtualFileSystem.Interfaces.ISOSVirtualFile;
import com.sos.VirtualFileSystem.Interfaces.ISOSVirtualFileSystem;
import com.sos.VirtualFileSystem.Interfaces.ISOSVirtualFolder;
import com.sos.VirtualFileSystem.Options.SOSConnection2OptionsAlternate;
import com.sos.VirtualFileSystem.common.SOSVfsBaseClass;
import com.sos.i18n.annotation.I18NResourceBundle;
/**
* \class SOSVfsZip
*
* \brief SOSVfsZip -
*
* \details
*
* \code
* .... code goes here ...
* \endcode
*
* <p style="text-align:center">
* <br />---------------------------------------------------------------------------
* <br /> APL/Software GmbH - Berlin
* <br />##### generated by ClaviusXPress (http://www.sos-berlin.com) #########
* <br />---------------------------------------------------------------------------
* </p>
* \author KB
* @version $Id: SOSVfsZip.java 14789 2011-07-08 15:51:52Z sos $23.08.2010
* \see reference
*
* Created on 23.08.2010 17:53:03
*/
/**
* @author KB
*
*/
@I18NResourceBundle(baseName = "SOSVirtualFileSystem", defaultLocale = "en")
public class SOSVfsZip extends SOSVfsBaseClass implements ISOSVfsFileTransfer, ISOSVFSHandler, ISOSVirtualFileSystem, ISOSConnection {
@SuppressWarnings("unused")
private final String conClassName = "SOSVfsZip";
private Logger logger = Logger.getLogger(SOSVfsZip.class);
@SuppressWarnings("unused")
private InputStream objInputStream = null;
@SuppressWarnings("unused")
private OutputStream objOutputStream = null;
private String strReplyString = "";
@SuppressWarnings("unused")
private ZipFile objWorkingDirectory = null;
public ZipOutputStream objZipOutputStream = null;
private String strZipArchiveName = "";
private String strCurrentEntryName = "";
private SOSVfsZipFileEntry objCurrentZipFileEntry = null;
public SOSVfsZip() {
//
}
/**
* \brief appendFile
*
* \details
* Appends a File, which pathname is given as a String-Parameter, to another
* file, with name is given as string-parameter.
*
* \return the size of the file after append-operation
*
* @param localFile
* @param remoteFile
* @return
*/
// TODO appendFile with ISOSVirtualFile
@Override
public long appendFile(String strSourceFileName, String strTargetFileName) {
JSFile objTargetFile = new JSFile(strTargetFileName);
long lngFileSize = 0;
try {
lngFileSize = objTargetFile.AppendFile(strSourceFileName);
}
catch (Exception e) {
e.printStackTrace(System.err);
String strM = "appendFile failed";
logger.error(strM, e);
throw new JobSchedulerException(strM, e);
}
return lngFileSize;
}
/**
* \brief ascii
*
* \details
*
* \return
*
*/
@Override
public void ascii() {
// nothing to do
}
/**
* \brief binary
*
* \details
*
* \return
*
*/
@Override
public void binary() {
// nothing to do
}
/**
* \brief changeWorkingDirectory
*
* \details
*
* \return
*
* @param pathname
* @return
* @throws IOException
*/
@Override
public boolean changeWorkingDirectory(String pstrPathName) {
boolean flgResult = true;
try {
if (objWorkingDirectory != null) {
objWorkingDirectory.close();
}
strZipArchiveName = pstrPathName;
JSFile objFile = new JSFile(pstrPathName);
if (this.isTarget() == true) {
if (objFile.exists()) {
objFile.delete();
}
objZipOutputStream = new ZipOutputStream(new FileOutputStream(pstrPathName));
flgResult = true;
logger.debug(String.format("Zip-File '%1$s' created", pstrPathName));
}
else {
if (objFile.exists()) {
objWorkingDirectory = new ZipFile(objFile, ZipFile.OPEN_READ);
flgResult = true;
logger.debug(String.format("Zip-File '%1$s' opened", pstrPathName));
}
}
}
catch (IOException e) {
e.printStackTrace(System.err);
logger.error("", e);
}
return flgResult;
}
/**
* \brief delete
*
* \details
*
* \return
*
* @param pathname
* @throws IOException
*/
@Override
public void delete(String pathname) throws IOException {
ZipEntry objZE = objWorkingDirectory.getEntry(pathname);
throw new JSNotImplementedException();
// objZE.delete();
}
private String getRealFileName(final String pstrPathname) {
// TODO use objWorkingDirectory if it is not null to determine the Directory
return pstrPathname;
}
/**
* \brief disconnect
*
* \details
*
* \return
*
* @throws IOException
*/
@Override
public void disconnect() throws IOException {
// nothing to do at all
}
/**
* \brief getFile
*
* \details
*
* \return
*
* @param remoteFile
* @param localFile
* @param append
* @return
* @throws Exception
*/
@Override
public long getFile(String pstrSourceFileName, String pstrTargetFileName, boolean append) throws Exception {
long lngFileSize = 0;
notImplemented();
return lngFileSize;
}
/**
* \brief getFile
*
* \details
*
* \return
*
* @param remoteFile
* @param localFile
* @return
* @throws Exception
*/
@Override
public long getFile(String remoteFile, String localFile) throws Exception {
return 0;
}
public ISOSVirtualFolder getFolder() {
// TODO implement VirtualFolder for ZIP
ISOSVirtualFolder objV = null;
return objV;
}
public ISOSVirtualFolder getFolder(final String pstrFolderName) {
// TODO implement VirtualFolder for ZIP
ISOSVirtualFolder objV = null;
return objV;
}
public Vector<ISOSVirtualFile> getFiles() {
Vector<ISOSVirtualFile> vecFiles = new Vector<ISOSVirtualFile>();
for (Enumeration<? extends ZipEntry> e = objWorkingDirectory.entries(); e.hasMoreElements();) {
ZipEntry entry = e.nextElement();
String strZipEntryName = entry.getName();
SOSVfsZipFileEntry objEntry = new SOSVfsZipFileEntry(strZipEntryName);
objEntry.setZipEntry(entry);
logger.debug(String.format("Zip-Entry '%1$s'", strZipEntryName));
vecFiles.add(objEntry);
}
return vecFiles;
}
public Vector<ISOSVirtualFile> getFiles(final String pstrFolderName) {
changeWorkingDirectory(pstrFolderName);
return getFiles();
}
/**
* \brief getHandler
*
* \details
*
* \return
*
* @return
*/
@Override
public ISOSVFSHandler getHandler() {
return this;
}
/**
* \brief getReplyString
*
* \details
*
* \return
*
* @return
*/
@Override
public String getReplyString() {
return strReplyString;
}
/**
* \brief isConnected
*
* \details
*
* \return
*
* @return
*/
@Override
public boolean isConnected() {
return true;
}
/**
* \brief listNames
*
* \details
*
* \return
*
* @param pathname
* @return
* @throws IOException
*/
@Override
public String[] listNames(String pathname) throws IOException {
Vector<String> objV = nList(pathname);
return (String[]) objV.toArray(new String[objV.size()]);
}
/**
* \brief login
*
* \details
*
* \return
*
* @param strUserName
* @param strPassword
*/
@Override
public void login(String strUserName, String strPassword) {
}
/**
* \brief logout
*
* \details
*
* \return
*
* @throws IOException
*/
@Override
public void logout() throws IOException {
}
/**
* \brief mkdir
*
* \details
* Create a new (sub-)Folder inside the ZIP-File.
*
* \return
*
* @param pathname
* @throws IOException
*/
@Override
public void mkdir(String pathname) throws IOException {
notImplemented();
// new File(pathname).mkdir();
}
/**
* \brief nList
*
* \details
*
* \return
*
* @param pathname
* @return
*/
@Override
public Vector<String> nList(String pathname) {
changeWorkingDirectory(pathname);
Vector<String> objV = new Vector<String>();
for (Enumeration<? extends ZipEntry> e = objWorkingDirectory.entries(); e.hasMoreElements();) {
ZipEntry entry = e.nextElement();
String strZipEntryName = entry.getName();
logger.debug(String.format("Zip-Entry '%1$s'", strZipEntryName));
objV.add(strZipEntryName);
}
return objV;
}
/**
* \brief nList
*
* \details
*
* \return
*
* @param pathname
* @param flgRecurseSubFolder
* @return
*/
@Override
public Vector<String> nList(String pathname, boolean flgRecurseSubFolder) {
return nList(pathname);
}
/**
* \brief nList
*
* \details
*
* \return
*
* @param recursive
* @return
* @throws Exception
*/
@Override
public Vector<String> nList(boolean recursive) throws Exception {
return nList();
}
/**
* \brief nList
*
* \details
*
* \return
*
* @return
* @throws Exception
*/
@Override
public Vector<String> nList() throws Exception {
notImplemented();
return null;
}
/**
* \brief passive
*
* \details
*
* \return
*
* @return
*/
@Override
public int passive() {
return 0;
}
/**
* \brief put
*
* \details
*
* \return
*
* @param localFile
* @param remoteFile
*/
@Override
public void put(String localFile, String remoteFile) {
}
/**
* \brief putFile
*
* \details
*
* \return
*
* @param localFile
* @param out
* @return
*/
@Override
public long putFile(String localFile, OutputStream out) {
return 0;
}
public void putFile(ISOSVirtualFile objVirtualFile) {
if (objZipOutputStream == null) {
throw new JobSchedulerException("No Zip Archive open for output");
}
String strName = objVirtualFile.getName();
strName = new File(strName).getAbsolutePath();
if (strName.startsWith("c:") == true) {
strName = strName.substring(3);
}
@SuppressWarnings("unused")
SOSVfsZipFileEntry objVF = (SOSVfsZipFileEntry) this.getFileHandle(strName);
InputStream objFI = objVirtualFile.getFileInputStream();
// TODO BufferSize as Option?
int lngBufferSize = 1024;
byte[] buffer = new byte[lngBufferSize];
int intBytesTransferred;
long totalBytes = 0;
try {
synchronized (this) {
while ((intBytesTransferred = objFI.read(buffer)) != -1) {
objZipOutputStream.write(buffer, 0, intBytesTransferred);
totalBytes += intBytesTransferred;
}
objFI.close();
// objZipOutputStream.flush();
// objZipOutputStream.finish();
objZipOutputStream.closeEntry();
logger.debug(String.format("put file '%1$s' to zip-file %2$s, no of Bytes %3$d", objVirtualFile.getName(), strZipArchiveName, totalBytes));
}
}
catch (Exception e) {
throw new JobSchedulerException("putfile reports exception", e);
}
finally {
}
}
/**
* \brief putFile
*
* \details
*
* \return
*
* @param localFile
* @param remoteFile
* @return
* @throws Exception
*/
@Override
public long putFile(String localFile, String remoteFile) throws Exception {
return 0;
}
/**
* \brief getConnection
*
* \details
*
* \return
*
* @return
*/
@Override
public ISOSConnection getConnection() {
return null;
}
/**
* \brief getSession
*
* \details
*
* \return
*
* @return
*/
@Override
public ISOSSession getSession() {
return null;
}
/**
* \brief mkdir
*
* \details
*
* \return
*
* @param pobjFolderName
* @return
* @throws IOException
*/
@Override
public ISOSVirtualFolder mkdir(SOSFolderName pobjFolderName) throws IOException {
notImplemented();
new File(pobjFolderName.Value()).mkdir();
return null;
}
/**
* \brief rmdir
*
* \details
*
* \return
*
* @param pobjFolderName
* @return
* @throws IOException
*/
@Override
public boolean rmdir(SOSFolderName pobjFolderName) throws IOException {
notImplemented();
new File(pobjFolderName.Value()).delete();
return true;
}
/**
* \brief dir
*
* \details
*
* \return
*
* @param pobjFolderName
* @return
*/
@Override
public SOSFileList dir(SOSFolderName pobjFolderName) {
String[] strEntryNames = getFilelist("", ".*", 1, true);
SOSFileList objFL = new SOSFileList();
objFL.add(strEntryNames, "");
return objFL;
}
/**
* \brief dir
*
* \details
*
* \return
*
* @param pathname
* @param flag
* @return
*/
@Override
public SOSFileList dir(String pathname, int flag) {
notImplemented();
// TODO Auto-generated method stub
return null;
}
@Override
public void setJSJobUtilites(JSJobUtilities pobjJSJobUtilities) {
}
@Override
public void ExecuteCommand(String strCmd) throws Exception {
// TODO shell-execute einbauen wg. pre- und pos-commands
}
@Override
public String createScriptFile(String pstrContent) throws Exception {
return EMPTY_STRING;
}
@Override
public Integer getExitCode() {
return 0;
}
@Override
public String getExitSignal() {
return EMPTY_STRING;
}
@Override
public StringBuffer getStdErr() throws Exception {
return new StringBuffer("");
}
@Override
public StringBuffer getStdOut() throws Exception {
return new StringBuffer("");
}
@Override
public boolean remoteIsWindowsShell() {
return false;
}
@Override
public ISOSConnection Authenticate(ISOSAuthenticationOptions pobjAO) throws Exception {
strReplyString = "230 Login successful.";
return this;
}
@Override
public void CloseConnection() throws Exception {
strReplyString = "ok";
}
@Override
public ISOSConnection Connect() throws Exception {
strReplyString = "ok";
return this;
}
@Override
public ISOSConnection Connect(ISOSConnectionOptions pobjConnectionOptions) throws Exception {
this.Connect();
return this;
}
@Override
public ISOSConnection Connect(String pstrHostName, int pintPortNumber) throws Exception {
return null;
}
@Override
public void CloseSession() throws Exception {
strReplyString = "221 Goodbye.";
}
@Override
public ISOSSession OpenSession(ISOSShellOptions pobjShellOptions) throws Exception {
return null;
}
@Override
public ISOSVirtualFile getFileHandle(String pstrFileName) {
if (pstrFileName.equalsIgnoreCase(strCurrentEntryName) == true) {
return objCurrentZipFileEntry;
}
/**
* the java-zip paket is not able to store utf-encoded characters
* but this translation will more or less getting deeper into problems
*/
// byte[] iso88591Data = pstrFileName.getBytes(Charset.forName ("UTF-8"));
// String strEncodedEntryName = new String(iso88591Data, Charset.forName ("ISO-8859-1"));
String strEncodedEntryName = pstrFileName;
// TODO Option KeepSourcePath
strEncodedEntryName = new File(strEncodedEntryName).getName();
SOSVfsZipFileEntry objF = new SOSVfsZipFileEntry(strEncodedEntryName);
ZipEntry objZE = null;
if (objZipOutputStream == null || this.isTarget() == false) {
objZE = objWorkingDirectory.getEntry(strEncodedEntryName);
if (objZE != null) {
objF.setHandler(this);
}
else {
objZE = objWorkingDirectory.getEntry(pstrFileName);
if (objZE != null) {
objF.setHandler(this);
}
}
}
else {
objZE = new ZipEntry(strEncodedEntryName);
try {
objZE.setComment("created by com.sos.SOSVirtualFileSystem.SOSVfsZip, locale " + Locale.getDefault());
objZE.setMethod(ZipEntry.DEFLATED);
objZipOutputStream.putNextEntry(objZE);
objF.objOutputStream = objZipOutputStream;
objF.objEntryOutputStream = objZipOutputStream;
objF.setHandler(this);
}
catch (IOException e) {
e.printStackTrace();
}
}
objF.setHandler(this);
objF.setZipEntry(objZE);
objCurrentZipFileEntry = objF;
strCurrentEntryName = pstrFileName;
return objF;
}
@Override
public boolean isNegativeCommandCompletion() {
return false;
}
@Override
public String[] getFilelist(String folder, String regexp, int flag, boolean withSubFolder) {
String[] strS = null;
try {
Vector<String> objV = new Vector<String>();
Pattern pattern = Pattern.compile(regexp, flag);
Enumeration<?> zipEntries = objWorkingDirectory.entries();
while (zipEntries.hasMoreElements()) {
String strEntryName = ((ZipEntry) zipEntries.nextElement()).getName();
Matcher matcher = pattern.matcher(strEntryName);
if (matcher.find()) {
objV.add(strEntryName);
}
} // while (zipEntries.hasMoreElements())
strS = (String[]) objV.toArray(new String[objV.size()]);
}
catch (Exception e) {
e.printStackTrace();
}
return strS;
}
@Override
public void CompletePendingCommand() {
// nothing to do
}
@Override
public ISOSConnection Connect(SOSConnection2OptionsAlternate pobjConnectionOptions) throws Exception {
// nothing to do
return null;
}
@Override
public String DoPWD() {
// TODO Auto-generated method stub
return null;
}
@Override
public OutputStream getAppendFileStream(String strFileName) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getFileSize(String strFileName) {
// TODO Auto-generated method stub
return 0;
}
@Override
public InputStream getInputStream(String strFileName) {
ZipEntry objZE = objWorkingDirectory.getEntry(strFileName);
InputStream objI = null;
if (objZE != null) {
try {
objI = objWorkingDirectory.getInputStream(objZE);
}
catch (IOException e) {
e.printStackTrace();
throw new JobSchedulerException();
}
}
return objI;
}
@Override
public String getModificationTime(String strFileName) {
// TODO Auto-generated method stub
return null;
}
@Override
public OutputStream getOutputStream(String strFileName) {
ZipOutputStream objZOS = null;
ZipEntry objZE = objWorkingDirectory.getEntry(strFileName);
if (objZE != null) {
try {
FileOutputStream objFOS = new FileOutputStream(objWorkingDirectory.getName());
objZOS = new ZipOutputStream(objFOS);
objZOS.putNextEntry(objZE);
}
catch (Exception e) {
e.printStackTrace();
throw new JobSchedulerException();
}
}
return objZOS;
}
@Override
public boolean isDirectory(String strFileName) {
// TODO Auto-generated method stub
return false;
}
@Override
public void rename(String strFileName, String pstrNewFileName) {
// TODO Auto-generated method stub
}
@Override
public void close() {
if (objWorkingDirectory != null) {
String strName = objWorkingDirectory.getName();
try {
objWorkingDirectory.close();
logger.debug(String.format("Zip-File '%1$s' closed", strName));
objWorkingDirectory = null;
}
catch (IOException e) {
e.printStackTrace();
throw new JobSchedulerException(String.format("Exception occured during close for '%1$s'", strName));
}
}
else {
this.closeOutput();
}
}
@Override
public void closeInput() {
// TODO Auto-generated method stub
}
@Override
public void closeOutput() {
if (objZipOutputStream != null) {
try {
objZipOutputStream.flush();
objZipOutputStream.close();
logger.debug(String.format("Zip Archive '%1$s' closed", strZipArchiveName));
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new JobSchedulerException("close failed", e);
}
}
}
@Override
public void flush() {
if (objZipOutputStream != null) {
try {
objZipOutputStream.flush();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new JobSchedulerException("flush failed", e);
}
}
}
@Override
public int read(byte[] bteBuffer) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int read(byte[] bteBuffer, int intOffset, int intLength) {
// TODO Auto-generated method stub
return 0;
}
@Override
public void write(byte[] bteBuffer, int intOffset, int intLength) {
// TODO Auto-generated method stub
}
@Override
public void write(byte[] bteBuffer) {
// TODO Auto-generated method stub
}
@Override
public void openInputFile(String pstrFileName) {
// TODO Auto-generated method stub
}
@Override
public void openOutputFile(String pstrFileName) {
// TODO Auto-generated method stub
}
@Override
public ISOSVirtualFile TransferMode(SOSOptionTransferMode pobjFileTransferMode) {
// TODO Auto-generated method stub
return null;
}
@Override
public void ControlEncoding(String pstrControlEncoding) {
// TODO Auto-generated method stub
}
}