When I try to create a java.util.zip.ZipFile I get a java.util.zip.ZipException: error in opening zip file. This exception only occurs when I try to open a large ZipFile (> 2GB). Is there a trick to open big zip files?
Later I need to extract single files from this zip and I doubt that the ZipInputStream is fast enough to extract the required files, since I need to run over all files.
Here is my StackTrace:
Caused by: java.util.zip.ZipException: error in opening zip file
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:225)
at java.util.zip.ZipFile.<init>(ZipFile.java:148)
at java.util.zip.ZipFile.<init>(ZipFile.java:162)
Update:
I found out that it works on my desktop computer and it works if I open the ZipFile as a JUnit-Test within Android Studio, too (Since JUnit-Tests are run on the local desktop computer and not on the android device). However, I could not get it working on the android device. I guess the reason is the android file system.
Key point to remember, especially if you are processing large zip archives is that, Java 6 only support zip file up to 2GB.
Java 7 supports zip64 mode, which can be used to process large zip file with size more than 2GB
Also using streams for big files is a good idea:
private static void readUsingZipInputStream() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(FILE_NAME));
final ZipInputStream is = new ZipInputStream(bis);
try {
ZipEntry entry;
while ((entry = is.getNextEntry()) != null) {
System.out.printf("File: %s Size %d Modified on %TD %n", entry.getName(), entry.getSize(), new Date(entry.getTime()));
extractEntry(entry, is);
}
} finally {
is.close();
}
Related
I am trying to write a program in Java to unzip files zipped by PKZIP tool in Mainframe. However, I have tried below 3 ways, none of them can solve my problem.
By exe.
I have tried to open it by WinRAR, 7Zip and Linux command(unzip).
All are failed with below error message :
The archive is either in unknown format or damaged
By JDK API - java.util.ZipFile
I also have tried to unzip it by JDK API, as this website described.
However, it fails with error message :
IO Error: java.util.zip.ZipException: error in opening zip file
By Zip4J
I also have tried to use Zip4J. It failed too, with error message :
Caused by: java.io.IOException: Negative seek offset
at java.io.RandomAccessFile.seek(Native Method)
at net.lingala.zip4j.core.HeaderReader.readEndOfCentralDirectoryRecord(HeaderReader.java:117)
... 5 more
May I ask if there is any java lib or linux command can extract zip file zipped by PKZIP in Mainframe? Thanks a lot!
I have successfully read files that were compressed with PKZip on z/OS and transferred to Linux. I was able to read them with java.util.zip* classes:
ZipFile ifile = new ZipFile(inFileName);
// faster to loop through entries than open the zip file as a stream
Enumeration<? extends ZipEntry> entries = ifile.entries();
while ( entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (!entry.isDirectory()) { // skip directories
String entryName = entry.getName();
// code to determine to process omitted
InputStream zis = ifile.getInputStream(entry);
// process the stream
}
}
The jar file format is just a zip file, so the "jar" command can also read such files.
Like the others, I suspect that maybe the file was not transferred in binary and so was corrupted. On Linux you can use the xxd utility (piped through head) to dump the first few bytes to see if it looks like a zip file:
# xxd myfile.zip | head
0000000: 504b 0304 2d00 0000 0800 2c66 a348 eb5e PK..-.....,f.H.^
The first 4 bytes should be as shown. See also the Wikipedia entry for zip files
Even if the first 4 bytes are correct, if the file was truncated during transmission that could also cause the corrupt file message.
I am working on a project in JavaFX. I need to download a file from the server for that I am using the ftp connection and downloading the file.
The size of the file is 560 MB, while downloading the file the code doesn't give any error but when I check the size of the file in the download location it is only 485 MB and I am not able to open it.
My code for downloading is:
OutputStream output = new FileOutputStream(toPath + "/" + dfile);
if(ftpClient.retrieveFile(dfile, output))
{
downloadButton.setDisable(true);
}
output.close();
Does java ftp have some download file size limit? How to resolve this problem? I have heard of chunking but don't know how to implement it in this case.
I downloaded the files in binary mode and it's working fine now.
ftpClient.setFileType(FTP.BINARY_FILE_TYPE)
I'm trying to transfer a pgp file with apache.commons.net.ftp.FTPClient, result seems successfully, but when I want to convert it to a txt file I encounter to this error:
gpg: [don't know]: invalid packet (ctb=20)
and when I check the exact size of downloaded file, I notice that it's size is about 1KB less than original file.
here is the code for downloading file:
FileOutputStream fos = new FileOutputStream(Localfilename);
InputStream inputStream = ftpClient.retrieveFileStream(remoteFileDir);
IOUtils.copy(inputStream, fos);
fos.flush();
IOUtils.closeQuietly(fos);
IOUtils.closeQuietly(inputStream);
boolean commandOK = ftpClient.completePendingCommand();
can any one understand what is mistake with my way or code?
[edited] noted that the original file decode (convert to txt)successfully, so the problem occures while downloading file.
[edited2] I run the program in my windows desktop and download file in windows, no problem for decode, and I understand that when I run my program with linux server this problem appears!
I found my problem!
The problem was with addressing the remote path, a silly mistake!
so If any one has this problem recheck and recheck again the addresses.
I have directory where I will look for ZIP files and then I will extract files from those ZIP files with TrueZIP 7. My problem is that when I have my application running, I cannot delete/edit file while application is running in Windows. I don't understand why this is happening, because when I'm accessing file in Windows my application is not doing anything with files, so why these files are locked to my Java app?
I have following code:
Path dir = FileSystems.getDefault().getPath(directory);
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path file : stream) {
// do something with file
}
} catch (IOException | DirectoryIteratorException x) {
System.err.println(x);
}
// Why those files are still locked to my app even when execution is here ???
I forgot to call unmount :
TVFS.umount();
Now I can delete/edit files.
Why does this code cause an error: access denied?
public void armazenaPerfil() throws FileNotFoundException, IOException {
FileOutputStream out = new FileOutputStream(this.login + "_perfil.mbk");
ObjectOutputStream objOut = new ObjectOutputStream(out);
objOut.writeObject(this);
System.out.println("Escrevi!");
objOut.close();
}
The error message:
ric93_perfil.mbk(acess denied)
java.io.FileNotFoundException
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:212)
at java.io.FileOutputStream.<init>(FileOutputStream.java:104)
at br.uefs.ecomp.myBook.model.Perfil.armazenaPerfil(Unknown Source)
Access denied problems are basically the operating system saying "You are not allowed to write that". Basically, an OS-level access control / permissions issue is preventing you from reading or writing the file at the specified location.
When you write a file using a relative pathname, the JVM will attempt to write it in a location relative to the running application's current working directory. What directory that will be depends on how the JVM is launched, but if you launch from a command prompt using the java command, it will be the command shell's current directory.
You can find out what the current director actually is using the one-liner suggested by Brendan Long:
System.out.println(new File(pathname).getAbsolutePath());
where pathname is the pathname of the file you were trying to read or write. Note that this doesn't actually check that the pathname refers to an existing file, or tell you that you should be able to create or open the file. It merely tells you what the absolute pathname for the file would be.