我有一个zip文件,其内容显示为byte [] 但原始文件对象不可访问.我想阅读每个条目的内容.我能够从字节的ByteArrayInputStream创建一个ZipInputStream,并可以读取条目及其名称.但是,我看不到一种简单的方法来提取每个条目的内容.
(我看过Apache Commons,但也看不到那么简单的方法).
UPDATE @ Rich的代码似乎解决了这个问题,谢谢
QUERY为什么两个例子的乘数都是*4(128/512和1024*4)?
我试图在一个zip文件中找到一个文件并将其作为一个文件InputStream.所以这就是我到目前为止所做的事情,我不确定我是否正确地做到了.
这是一个样本,因为原件略长,但这是主要组件......
public InputStream Search_Image(String file_located, ZipInputStream zip)
throws IOException {
for (ZipEntry zip_e = zip.getNextEntry(); zip_e != null ; zip_e = zip.getNextEntry()) {
if (file_located.equals(zip_e.getName())) {
return zip;
}
if (zip_e.isDirectory()) {
Search_Image(file_located, zip);
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
现在我面临的主要问题是,ZipInputStreamin Search_Image与原始组件相同ZipInputStream......
if(zip_e.isDirectory()) {
//"zip" is the same as the original I need a change here to find folders again.
Search_Image(file_located, zip);
}
Run Code Online (Sandbox Code Playgroud)
现在问题是,如何获得ZipInputStream新的zip_entry?如果我在我的方法中做了任何错误,请加入,因为我仍然缺乏这个类的逻辑.
需要使用流的分段上传方式将大文件上传到AWS S3,而不是使用lambda的/tmp。文件已上传但未完全上传。
在我的情况下,zip中每个文件的大小无法预测,可能一个文件的大小可达1 Gib。所以我使用ZipInputStream从S3读取,我想将其上传回S3。因为我正在研究lambda ,由于文件太大,我无法将文件保存在 lambda 的 /tmp 中。因此我尝试使用 S3 分段上传直接读取并上传到 S3,而不保存在 /tmp 中。但我遇到了一个问题,文件没有完全写入。我怀疑文件每次都被覆盖。请查看我的代码并提供帮助。
public void zipAndUpload {
byte[] buffer = new byte[1024];
try{
File folder = new File(outputFolder);
if(!folder.exists()){
folder.mkdir();
}
AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();
S3Object object = s3Client.getObject("mybucket.s3.com","MyFilePath/MyZip.zip");
TransferManager tm = TransferManagerBuilder.standard()
.withS3Client(s3Client)
.build();
ZipInputStream zis =
new ZipInputStream(object.getObjectContent());
ZipEntry ze = zis.getNextEntry();
while(ze!=null){
String fileName = ze.getName();
System.out.println("ZE " + ze + " : " + fileName);
File newFile = new File(outputFolder + File.separator + fileName);
if …Run Code Online (Sandbox Code Playgroud) 我有一个ZipInputStream,其中包含我想要应用转换的许多XML文件.下面的代码加载XSLT和ZIP文件并循环遍历ZIP条目,尝试将转换应用于每个条目.但是,转换函数似乎在执行转换后关闭输入流,导致getNextEntry()函数失败,因为流已关闭.
是否有一个简单的方法解决这个问题(保持输入流打开,允许ZipInputStream移动到下一个条目)或者我错过了更基本的东西吗?
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(xsltFileName));
FileInputStream fis = new FileInputStream(fileName);
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry ze = null;
while ((ze = zis.getNextEntry()) != null)
{
String newFileName = ze.getName();
transformer.transform(new StreamSource(zis), new StreamResult(new FileOutputStream(newFileName)));
}
Run Code Online (Sandbox Code Playgroud)
我试图寻找一个解决方案,但似乎没有想出任何有意义的东西.我很感激任何想法或反馈.
我正在尝试从嵌套的 zip 存档中提取文件并在内存中处理它们。
这个问题不是关于什么的:
如何在Java中读取zip文件:不,问题是如何读取zip文件中的zip文件,等等(如嵌套zip文件)。
将临时结果写入磁盘:不,我问的是在内存中完成这一切。我使用将结果临时写入磁盘的不太有效的技术找到了许多答案,但这不是我想要做的。
例子:
Zip 文件 -> Zipfile1 -> Zipfile2 -> Zipfile3
目标:提取每个嵌套 zip 文件中找到的数据,所有数据都在内存中并使用 Java。
你说ZipFile就是答案?不,不是,它适用于第一次迭代,即:
Zip 文件 -> Zipfile1
但是一旦你到达 Zipfile2,并执行:
ZipInputStream z = new ZipInputStream(zipFile.getInputStream( zipEntry) ) ;
Run Code Online (Sandbox Code Playgroud)
你会得到一个 NullPointerException。
我的代码:
public class ZipHandler {
String findings = new String();
ZipFile zipFile = null;
public void init(String fileName) throws AppException{
try {
//read file into stream
zipFile = new ZipFile(fileName);
Enumeration<?> enu = zipFile.entries();
exctractInfoFromZip(enu);
zipFile.close();
} catch (FileNotFoundException e) { …Run Code Online (Sandbox Code Playgroud) 当我打开文件以解压缩其内容时,出现以下异常。当我在 Windows 资源管理器中选择文件或将鼠标悬停在显示工具提示的文件上时,就会发生这种情况。
System.IO.IOException was unhandled
Message=The process cannot access the file 'D:\Documents\AutoUnZip\Zips\MVCContrib.Extras.release.zip' because it is being used by another process.
Source=mscorlib
StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at System.IO.File.OpenRead(String path)
at AutoUnzip.SelectFolderForm.w_Changed(Object sender, FileSystemEventArgs e) in D:\Projects\WindowsForms\AutoUnzip\AutoUnzip\SelectFolderForm.cs:line 37
at System.IO.FileSystemWatcher.OnCreated(FileSystemEventArgs e)
at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32 action, String name)
at …Run Code Online (Sandbox Code Playgroud) java ×5
zip ×2
amazon-s3 ×1
c# ×1
ioexception ×1
sharpziplib ×1
transform ×1
xslt ×1
zipfile ×1