我试图使用java exec实用程序生成7z文件,但它将创建空的zip文件

时间:2015-08-13 07:15:32

标签: java 7zip

package com.otp.util;

import java.io.FileWriter; import java.io.IOException; import
java.text.SimpleDateFormat; import java.util.Date;

import com.otp.servlets.MessageServlet;

public class CDRWriter {        
public FileWriter fileWriter = null;    
static int lineCounter = 0;
static String fileName = null;
public void writeCDR(String cdrData) throws IOException {
    if(lineCounter == 0){ 
        fileName = createFile(); 
    }else if(lineCounter>500){
        String temp=fileName;
        fileName = createFile();
        lineCounter=0;
        Runtime rt = Runtime.getRuntime();
        String zipCmd="7z a "+"\""+MessageServlet.filePath+temp+".7z"+"\""+" "+"\""+MessageServlet.filePath+temp+"\"";
        System.out.println("zipCmd = "+zipCmd);
        rt.exec(zipCmd);            
            //rt.exec("del "+MessageServlet.filePath+temp);
    }
    System.out.println("cdr data = "+cdrData);
    try {
        if(lineCounter == 0){
            fileWriter = new FileWriter(MessageServlet.filePath+fileName);
        }else{
            fileWriter = new FileWriter(MessageServlet.filePath+fileName,true);
        }
                System.out.println("cdr after if else condition ="+cdrData);
                fileWriter.write(cdrData.toString());
                System.out.println("cdr after write method ="+cdrData);
                fileWriter.write("\r\n");
                fileWriter.flush();
                //fileWriter.close();
                lineCounter++;
                System.out.println("CDRWriter : lineCounter = "+lineCounter);       } catch (IOException e) { 
        e.printStackTrace(); 
        }
    }// end of WriterCDR method 
public String createFile() throws IOException   {
    SimpleDateFormat sdf = new
    SimpleDateFormat("dd-MM-yyyy-HH-mm-ss");
    String fileName ="GSMS_CDR_"+ sdf.format(new Date())+".txt" ;
    return fileName; 
}// end of the createFile method

}// end of CDRWriter class

1 个答案:

答案 0 :(得分:0)

我会做那样的事情:

import java.io.*;
import SevenZip.Compression.LZMA.*;

public class Create7Zip
{
  public static void main(String[] args) throws Exception
  {

    // file to compress
    File inputToCompress = new File(args[0]);
    BufferedInputStream inputStream  = new BufferedInputStream(new java.io.FileInputStream(inputToCompress));
    // archive
    File compressedOutput = new File(args[1] + ".7z");
    BufferedOutputStream outputStream = new BufferedOutputStream(new java.io.FileOutputStream(compressedOutput));


    Encoder encoder = new Encoder();
    encoder.SetAlgorithm(2);
    encoder.SetDictionarySize(8388608);
    encoder.SetNumFastBytes(128);
    encoder.SetMatchFinder(1);
    encoder.SetLcLpPb(3,0,2);
    encoder.SetEndMarkerMode(false);
    encoder.WriteCoderProperties(outputStream);

    long fileSize;
    fileSize = inputToCompress.length();
    for (int i = 0; i < 8; i++)
    {
      outputStream.write((int) (fileSize >>> (8 * i)) & 0xFF);
    }

    encoder.Code(inputStream, outputStream, -1, -1, null);

    // free resources
    outputStream.flush();
    outputStream.close();
    inputStream.close();
  }
}

SevenZip套装的SKD来自官方SKD。下载here;)。

免责声明:我相信,我不久前在网上发现了这个片段......但是我找不到源了。