首页 > 解决方案 > Java 程序正在生成空的 zip 文件

问题描述

我有一个程序。这会在目录中写入一个文件,然后使用该写入的文件将其添加到 Zip 文件中。

现在,文件已成功生成。但是 Zip 文件正在生成空。我还检查了它的文件名的 zip 条目,因为它没有将条目放入带有绝对文件路径的 zip 中。

但问题仍然存在。任何帮助将不胜感激 :)

邮编生成方法

// Function to write files in ZIP archive..
public void writeToZip( String ibftDirectory, ZipOutputStream zipOS ) throws Exception {

        LOGGER.println( "Writing file = " + ibftDirectory + " to zip file" );

        // Initialize File..
        File file = new File( ibftDirectory );  

        // Open stream of file..
        FileInputStream fis = new FileInputStream( file );

        // Get entry name..
        String entryName = getFileName( ibftDirectory );

        // Create zip entry..
        ZipEntry zipEntry = new ZipEntry( entryName );

        // Add zip entry...
        zipOS.putNextEntry( zipEntry );

        // Create byte array..
        byte[] bytes = new byte[ 1024 ];

        // Length of stream holder..
        int length;

        // Write to zip..
        while ( ( length = fis.read( bytes ) ) >= 0 ) {

            zipOS.write( bytes, 0, length );

        }

        LOGGER.println( "File = " + ibftDirectory + " added to zip successfully!" );

        // Close streams to avoid memory leaks..
        zipOS.closeEntry();

     fis.close();   

}

邮政编码

public class UploadFileWithSecurity extends IbftUtil implements IConstants {

    private FileOutputStream fileOS = null;
    private ZipOutputStream zipOS = null;  

public void uploadFiles ( String filename, String filePaymentRecord, String ibftDirectoryPath ) throws Exception {

        // To overcome the IBM JCE Limitation..
        Security.addProvider( new org.bouncycastle.jce.provider.BouncyCastleProvider() );

        setKeys();

        String signatureFileName = "sign_" + filename.replace(".csv", ".sig");
        String keyFileName = "key_" + filename.replace(".csv", ".key");

        try {

            fileOS = new FileOutputStream( ibftDirectoryPath + filename.replace( ".csv" , ".zip") );
            zipOS = new ZipOutputStream( fileOS );      

        }
        catch ( Exception e ) {
            LOGGER.println( "Exception in outputstream of Zip file.. " + e );
        }
        String sign = sign( filehash , privateKeySender );

        writeFile( sign, signatureFileName, ibftDirectoryPath );

        try {

            writeToZip( ibftDirectoryPath + signatureFileName, zipOS );

        }
        catch ( Exception e ) {

            LOGGER.println( "Exception in writing of Zip file.. " + e );

        }
String encryptedKey = RSAEncrypt( AESParams.getKey().getEncoded(), "Key" );     

        LOGGER.println( LOG + " symmetric key encrypted.. " );

        // Write the encrypted symmetric key in a file..
        writeFile( encryptedKey, keyFileName, ibftDirectoryPath );

        try {

            writeToZip( ibftDirectoryPath + keyFileName, zipOS );

            zipOS.close();
            fileOS.close();

        }
        catch ( Exception e ) {

            LOGGER.println( "Exception in writing of Zip file.. " + e );

        }
    }
}

获取文件名函数

// Get file name... 
public String getFileName ( String path ) {

    String filename = path.substring( path.lastIndexOf("/") + 1 );      

    LOGGER.println( "Filename extracted = " + filename );

    return filename;
 }

标签: java

解决方案


推荐阅读