how to get file size in truzip

139 Views Asked by At

I am using truezip version (7.7.9) to update a archive file. The way I am doing it is as follows

File entry = new TFile(filepath);
writer = new TFileWriter(entry);
writer.append("MyString");
writer.flush();
long fileSize = entry.length(); // which always gives value as 0

I need the exact file size for some purpose But this always gives 0 Is there any other way I can get that.

I read the documentation of TFile class\ https://truezip.java.net/apidocs/de/schlichtherle/truezip/file/TFile.html#length() couldn't quite understand what it does

3

There are 3 best solutions below

0
Ian2thedv On BEST ANSWER

From the FAQ:

Every now and then you might want to treat an archive file like a regular file rather than a virtual directory. For example, when trying to obtain the length of the archive file in bytes. You would normally do this by calling the method File.length(). However, if the File object is an instance of the TFile class and the path has been detected to name a valid archive file, then this method would always return zero. This is because you might have changed the archive file and then it would be impossible to return a precise result until the changes have been committed to the target archive file.

It seems you can either compute the size of the file before changing it and then add the size of what you are appending, or you need to write the file and then call File.length(). You can do this by calling TVFS.unmount() or TVFS.unmount(TFile)

0
Laiv On

See this article at official page

The API should not detect an individual archive file as a virtual directory. How can I do this

Look at this

[...] However, if the File object is an instance of the TFile class and the path has been detected to name a valid archive file, then this method would always return zero. This is because you might have changed the archive file and then it would be impossible to return a precise result until the changes have been committed to the target archive file..

It also applies on TPath

I wonder if you can use Files.size(path) in order to get what you need (after & before changes on your TZip)

Hope It will help.

0
SkyWalker On

If the archive file is valid, then it returns 0. If there is any problem occurs then it will give IOException.

If you change anything, then you need to call the method TFile.umount() to commit all changes.

Then use the following method to obtain a TFile which does not detect the archive file and call its length() method:

In TrueZIP 7.5, you can just call TFile.toNonArchiveFile()

// Note that the actual path may refer to anything, even a nested archive file.
TFile inner = new TFile("outer.zip/inner.zip");
TFile file = inner.toNonArchiveFile(); // convert - since TrueZIP 7.5
... // there may be some I/O here
TVFS.umount(inner); // unmount potential archive file
// Now you can safely do any I/O to $file.
long length = file.length();

You can do it by another way also:

private static TFile newNonArchiveFile(TFile file) {
    return new TFile(file.getParentFile(), file.getName(), TArchiveDetector.NULL);
}

Resource Link: How can you get the size of a file in an archive using TrueZip?