java - 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;
}
解决方案
推荐阅读
- types - 这是功能亚型关系的一个例子吗?
- r - Pivot_longer 两个列在 R 中混合字符和数字
- java - 除非整个执行结束,否则不会生成 testNG 报告
- docker - 从非 Docker 应用程序访问 Dockercontainer 内的 MariaDB 数据库的正确方法是什么?
- c# - 如何添加“事件动作
“到字典? - c# - 由于字段名称,无法反序列化当前 JSON 数组
- c# - 在 ASP .NET Core 中更改上传的 IFormfile 的名称
- linux - 检查标准输入是否在busybox脚本ash或bash中有数据
- javascript - 如何通过chrome的控制台截取html5视频的当前帧并将其复制到剪贴板
- python - Fastapi/Tortoise 早期模型初始化