 /*
 * Copyright (C) 11-sept.-2003  Jean-Yves Beaujean 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 * This program 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 General Public License for
 * more details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,  59
 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * 
 */
package jxtray.io;


import java.io.IOException;
import java.io.FileOutputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;


/**
 * @author Jean-Yves Beaujean
 *
 * This class allows to write a String in an zip file.
 *  
 */
public class ZipWriterStrategy extends WriterStrategy
{
	private ZipOutputStream zipOutput = null;
	public ZipWriterStrategy(String filename)
	{
		this._fileName = filename;
	}
	
	/** Return the writer name 
	 *  @return String
	 */	
	public String getName() { return "Zip Writer";}
		
	public void open() throws IOException
	{
		if ( _fileName != null  )
		{
			zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(_fileName.substring(0,_fileName.lastIndexOf("."))+".zip")) );
			zipOutput.setMethod(ZipOutputStream.DEFLATED);
			ZipEntry entry = new ZipEntry((new File(_fileName)).getName());
			entry.setMethod(ZipOutputStream.DEFLATED);
			zipOutput.putNextEntry(entry);
		}
	}
	
	public void close() throws IOException
	{
		if ( _fileName != null && zipOutput != null)
		{
			zipOutput.closeEntry();
			zipOutput.close();
		}
	}	
	
	/** Write the data <code>data</code> in file <code>file</file>
	 *  It use <code>ZipOutputStream</code>
	 *  @throws IOException
	 */		
	public void write ( String data) throws IOException
	{
			zipOutput.write(data.getBytes());
	}

	
}
