I am trying to implement program to zip and unzip a file. All I want to do is to zip a file (fileName.fileExtension) with name as fileName.zip and on unzipping change it again to fileName.fileExtension.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
This is how I used to rename files or change its extension.
edit: John's method renames the file (keeping the extension). To change the extension do:
This only changes the last extension to a filename, i.e. the
.gz
part ofarchive.tar.gz
. Therfore it works fine with linux hidden files, for which the name starts with a.
This is quite safe because ifgetParent()
returnsnull
(i.e. in the event of the parent being the system root) it is "cast" to an empty String as the whole argument to the File constructor is evaluated first.The only case where you will get a funny output is if you pass in a File representing the system root itself, in which case the
null
is prepended to the rest of the path string.I would check, if the file has an extension before changing. The solution below works also with files without extension or multiple extensions
Try with:
I want to avoid the new extension just happening to be in the path or filename itself. I like a combination of java.nio and apache StringFilenameUtils.
By the same logic as mentioned @hsz, but instead simply use replacement: