Package net.i2p.util
Class FileUtil
java.lang.Object
net.i2p.util.FileUtil
General helper methods for messing with files
These are static methods that do NOT convert arguments
to absolute paths for a particular context and directory.
Callers should ALWAYS provide absolute paths as arguments,
and should NEVER assume files are in the current working directory.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
static boolean
static boolean
static boolean
extractZip
(File zipfile, File targetDir) As of release 0.7.12, any files inside the zip that have a .jar.pack or .war.pack suffix are transparently unpacked to a .jar or .war file using unpack200.static boolean
extractZip
(File zipfile, File targetDir, int logLevel) Warning - do not call any new classes from here, or update will crash the JVM.static boolean
Public since 0.8.3static void
Usage: FileUtil (delete path | copy source dest | rename from to | unzip path.zip)static void
readFile
(String path, String root, OutputStream out) Dump the contents of the given path (relative to the root) to the output stream.static String
readTextFile
(String filename, int maxNumLines, boolean startAtBeginning) Read in the last few lines of a (newline delimited) textfile, or null if the file doesn't exist.static boolean
Try to rename, if it doesn't work then copy and delete the old.static final boolean
Delete the path as well as any files or directories underneath it.static final boolean
Delete the path as well as any files or directories underneath it.static boolean
Verify the integrity of a zipfile.
-
Constructor Details
-
FileUtil
public FileUtil()
-
-
Method Details
-
rmdir
Delete the path as well as any files or directories underneath it.- Parameters:
path
- path to the directory being deletedfailIfNotEmpty
- if true, do not delete anything if the directory is not empty (and return false)- Returns:
- true if the path no longer exists (aka was removed), false if it remains
-
rmdir
Delete the path as well as any files or directories underneath it.- Parameters:
target
- the file or directory being deletedfailIfNotEmpty
- if true, do not delete anything if the directory is not empty (and return false)- Returns:
- true if the path no longer exists (aka was removed), false if it remains
-
extractZip
As of release 0.7.12, any files inside the zip that have a .jar.pack or .war.pack suffix are transparently unpacked to a .jar or .war file using unpack200. Logs at WARN level to wrapper.log -
extractZip
Warning - do not call any new classes from here, or update will crash the JVM.- Parameters:
logLevel
- Log.WARN, etc.- Returns:
- true if it was copied successfully
- Since:
- 0.9.7
-
verifyZip
Verify the integrity of a zipfile. There doesn't seem to be any library function to do this, so we basically go through all the motions of extractZip() above, unzipping everything but throwing away the data. Todo: verify zip header? Although this would break the undocumented practice of renaming the i2pupdate.sud file to i2pupdate.zip and letting the unzip method skip over the leading 56 bytes of "junk" (sig and version)- Returns:
- true if ok
-
isPack200Supported
public static boolean isPack200Supported()Public since 0.8.3- Since:
- 0.8.1
-
readTextFile
Read in the last few lines of a (newline delimited) textfile, or null if the file doesn't exist. Warning - this inefficiently allocates a StringBuilder of size maxNumLines*80, so don't make it too big. Warning - converts \r\n to \n Warning - inefficient if startAtBeginning is false, see console LogsHelper for better version- Parameters:
maxNumLines
- max number of lines (or -1 for unlimited)startAtBeginning
- if true, read the first maxNumLines, otherwise read the last maxNumLines- Returns:
- string or null; does not throw IOException.
-
readFile
Dump the contents of the given path (relative to the root) to the output stream. The path must not go above the root, either - if it does, it will throw a FileNotFoundException Closes the OutputStream out on successful completion but leaves it open when throwing IOE.- Throws:
IOException
-
copy
- Returns:
- true if it was copied successfully
-
copy
- Parameters:
quiet
- don't log fails to wrapper log if true- Returns:
- true if it was copied successfully
-
copy
- Parameters:
quiet
- don't log fails to wrapper log if true- Returns:
- true if it was copied successfully
- Since:
- 0.8.8
-
rename
Try to rename, if it doesn't work then copy and delete the old. Always overwrites any existing "to" file. Method moved from SingleFileNamingService.- Returns:
- true if it was renamed / copied successfully
- Since:
- 0.8.8
-
main
Usage: FileUtil (delete path | copy source dest | rename from to | unzip path.zip)
-