Renaming a File/Folder inside a Zip File in Java?
Zip is an archive format, so mutating generally involves rewriting the file.
Some particular features of zip also get in the way (zip is full of "features"). As well as the central directory at the end of the archive, each component file is preceded by its file name. Zip doesn't have a concept of directories - file names are just strings that happen to include "/"
characters (and substrings such as "../"
.
So, you really need to copy the file using ZipInputStream
and ZipOutputStream
, renaming as you go. If you really wanted to you could rewrite the file in place doing your own buffering. The process does cause the contents to be recompressed as the standard API has no means of obtaining the data in compressed form.
Edit: @Doval points out that @megasega's answer uses Zip File System Provider in NIO, new (relative to this answer) in Java SE 7. It's performance will likely be not great, as were the archive file systems in RISC OS' GUI of thirty years ago.
DotNetZip - rename file entry in zip file while compressing
From the DotNetZip FAQ:
Add an entry, overriding its name in the archive
using (ZipFile zip1 = new ZipFile())
{
zip1.AddFile("myFile.txt").FileName = "otherFile.txt";
zip1.Save(archiveName);
}
Renaming zip to match it's contents using PowerShell
This should help you here's a function to get the name of the zip file based on your requirements, just pass it the location of your log files:
function Get-ZipFileName
{
param
($logPath)
# Get a list of files, remove the extension and split on the hyphen, #
# then take the second part of the array
$filenames = gci -Path $logPath -Filter "*.log" | % { $($_.Basename -split "-")[1] }
# Get the first item in the now sorted list
$first = $filenames | Sort-Object | Select-Object -First 1
# Get the first item in the sorted list, now descending to get the last
$last = $filenames | Sort-Object -descending | Select-Object -First 1
# return the filename
"log-{0} - {1}" -f $first, $last
}
I would suggest you look at using a slightly different date format that will sort better, yyyymmdd will sort better than ddmmyyyy.
Related Topics
Search Ip from a Text File in .Csv Log File, If Found Add New Column Next to It
Why Does Automating Sftp with Expect Hang After Sending The Password
Karma Not Found from Maven After Re-Install
Perl-Mechanize Runs into Limitations - Several Debugging Attempts Started
Why Proc Process Faster Than Others
Why Am I Getting an "Implicit Declaration of Function 'Ndo_Get_Stats' " Error
What's The Best Way to Move a Directory into Place in a Makefile Install
Putting Extensions in a Certificate in Openssl
Watchman Makes Fsnotify Spuriously Detect File Changes
Sorting in Fortran, Undefined Reference to Qsort_
Debian Sources.List.D Versus Sources.List
Why Does Sed Leave Many Files Around
How to Make a Cross Compiler Using Gcc