0

这是我所拥有的,但文件不断覆盖以前的信息。有没有办法只压缩文件并将文件写入文本文件而无需将文件物理放入文件夹中。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.*; 

public class cuscreport 
{
    List<String> fileList;
    private static final String INPUT_ZIP_FILE = "c:\\cusc031513.zip";
    private static final String OUTPUT_FOLDER = "C:\\temp\\test";
    Vector<String> v =new Vector<String>();

    public static void main( String[] args ) throws IOException
    {
        cuscreport unZip = new cuscreport();
        unZip.unZipIt(INPUT_ZIP_FILE,OUTPUT_FOLDER);
    }
    private  final static String getDate(  )   {  
        DateFormat df = new SimpleDateFormat( "yyyy-MM-dd" ) ;  
        df.setTimeZone( TimeZone.getTimeZone( "EST" )  ) ;  
        return ( df.format( new Date(  )  )  ) ;  
    }  

    /**
     * Unzip it
     * @param zipFile input zip file
     * @param output zip file output folder
     */
    public void unZipIt(String zipFile, String outputFolder) throws IOException
    {

        byte[] buffer = new byte[1024];

     try{

        //create output directory is not exists
        File folder = new File(OUTPUT_FOLDER);
        if(!folder.exists())
        {
            folder.mkdir();
        }

        //get the zip file content
        ZipInputStream zis = 
            new ZipInputStream(new FileInputStream(zipFile));
        //get the zipped file list entry
        ZipEntry ze= zis.getNextEntry();




        while(ze!=null){

           String fileName = ze.getName();
           File newFile = new File(outputFolder + File.separator + fileName);

           System.out.println("file unzip : "+ newFile.getAbsoluteFile());
           v.add(fileName);

            //create all non exists folders
            //else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();

           FileOutputStream fos = new FileOutputStream(newFile);             

            int len;
            while ((len = zis.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
            }

            fos.close(); 
            ze = zis.getNextEntry();
        }

        zis.closeEntry();
        zis.close();

        for(int x = 1; x < v.size()-1; x++ )
        {
             System.out.println(v.get(x));
             File inputFile = new File( outputFolder + File.separator +v.get(x));
             FileReader in = new FileReader(inputFile);
             File outputFile = new File("C:\\temp\\test\\cuscreport"+getDate()+".txt");
             int c;
             FileWriter out = new FileWriter(outputFile);
             while ((c = in.read()) != -1)
             {
                    out.write(c);
             }

                 in.close();
                 out.close();
        }



        System.out.println("Done");

    }catch(IOException ex){
       ex.printStackTrace(); 
    }
   }    
}

这是我所拥有的,但文件不断覆盖以前的信息。有没有办法只压缩文件并将文件写入文本文件而无需将文件物理放入文件夹中。

4

1 回答 1

0

如果您查看FileWriter 的 JavaDocs,您会看到它有一个用于附加到文件的附加变量

于 2013-03-19T21:53:44.417 回答