Log dump and compression is very critical, it can not only reduce the disk space consumption, but also in the event of a failure to locate the cause of the log. Here's a look at the file dump implementation of Golang and Java.
Go language:
Using the Walk method under the FilePath package, you can refer to the history article:
Go Language Path/filepath Package Walk source code Analysis
Package Mainimport ("FMT" "OS" "IO" "archive/zip" "Path/filepath" "Time" "Log") func main () {logFile: = "D:/tmp/successlo G/logs/root.log "Backfile: =" d:/tmp/successlog/logs/root_ "+ time. Now (). Format ("20060102150405") + ". Zip" err: = ZipFile (LogFile, backfile) if err! = Nil {log. Println (FMT. Sprintf ("Zip file%s to%s error:%v", LogFile, Backfile, err)) return} else {OS. Remove (LogFile)}//Dump create new file after//createfile ()//Modify file Permissions//os. Chmod (Backfile, 0400)//delete backup file//deleteoldbackfiles (dir)}func ZipFile (source, target string) error {ZipFile, err: = OS. OpenFile (target, OS. O_append|os. O_create|os. O_wronly, 0440) if err! = Nil {log. PRINTLN (ERR) return err} defer Zipfile.close () Archive: = Zip. Newwriter (ZipFile) defer archive. Close () return filepath. Walk (source, func (path string, info OS). FileInfo, err Error) error {if Err! = Nil {return err} header, err: = Zip. Fileinfoheader (info) If err! = Nil {return err} if!info. Isdir () {header. Method = zip. DefLate} header. Setmodtime (time. Now (). UTC ()) header. Name = Path writer, err: = archive. CreateHeader (header) If err! = Nil {return err} if info. Isdir () {return nil} file, err: = OS. Open (PATH) if err! = Nil {return err} defer file. Close () _, err = Io. Copy (writer, file) return err})}
Go compression results
Java Edition:
See note for instructions.
Import Org.slf4j.logger;import org.slf4j.loggerfactory;import Java.io.*;import Java.text.dateformat;import Java.text.simpledateformat;import Java.util.zip.crc32;import Java.util.zip.checkedoutputstream;import Java.util.zip.zipentry;import java.util.zip.zipoutputstream;/*** @program: website* @description: Dump Compressed file * @author: smallsoup* @create: 2018-08-12 17:58**/public class ZipFile {private static final Logger Logger = Loggerfactory.getlogg ER (zipfile.class); /** * Format File name format */private static final String Audit_log_format = "Yyyymmddhhmmsssss"; /** * Compressed file suffix */private static final String Audit_file_zip_suffix = ". ZIP"; /** * Pre-compressed file suffix */private static final String Audit_file_ext = ". Log"; private static final int zip_buffer = 4096; /** * Controls whether the compressed file after decompression with the base path */private static final String RootPath = ""; public static void Main (string[] args) throws IOException {System.out.println (); New ZipFile (). Zipauditlogfile ("D:/tmp/successloG/logs/root.log "); /** * Log Compression * * @param waitzipfile to compress file name * @throws IOException */private void Zipauditlogfile (String Waitzipfile) throws IOException {file OldFile = new File (waitzipfile); if (!oldfile.exists ()) {logger.error ("Zipauditlogfile name is {} not exist", waitzipfile); Return }//Generate ZIP file name DateFormat DataFormat = new SimpleDateFormat (Audit_log_format); String formattime = Dataformat.format (oldfile.lastmodified ()); int end = Waitzipfile.length ()-audit_file_ext.length (); String zipfilename = waitzipfile.subsequence (0, end) + "_" + Formattime + audit_file_zip_suffix; File ZipFile = new file (zipfilename); FileOutputStream Zipfos = null; Zipoutputstream zipos = null; Checkedoutputstream cos = null; try {Zipfos = new FileOutputStream (ZipFile); cos = new Checkedoutputstream (Zipfos, New CRC32 ()); Zipos = new Zipoutputstream (cos); Compress (OldFile, Zipos, RootPath); if (zipfile.exists ()) {//the log file permission to finish is changed to try {//linux], it needs to be installed on Windows Cyg Win and add the Cygwin Bin directory to the environment variable path in order to Runtime.getruntime (). EXEC ("chmod 400-r" + zipfile); Delete old files after compression Boolean isdelete = Oldfile.delete (); Create a new file if (isdelete) {oldfile.createnewfile (); }//Boolean issuccess = Pathutil.setfilepermision (Zipfile.topath (), archive_logfile_permision);// Logger.warn ("Set archive file: {}, permision result is {}", Zipfile.getabsolutepath (), issuccess); } catch (IOException e) {logger.error ("Set archive file:{} permision catch an error: {}", ZipFile , e); }}} finally {if (null! = Zipos) {zipos.close (); } if (null! = Cos) {cos.close (); } if (null! = Zipfos) {zipfos.close (); }}}/** * compress file or directory * * @param oldFile file to compress * @param zipout Compressed file stream * @param baseDir BaseDir * @throws IOException */private void compress (File oldFile, Zipoutputstream zipout, String baseDir) throws Ioexcepti on {if (Oldfile.isdirectory ()) {compressdirectory (OldFile, ZipOut, BaseDir); } else {compressfile (oldFile, ZipOut, BaseDir); }}/** * Compressed directory * * @param dir to compress directory * @param zipout compressed file stream * @param baseDir baseDir * @throws I Oexception */private void Compressdirectory (File dir, Zipoutputstream zipout, String baseDir) throws IOException { file[] files = dir.listfiles (); for (file file:files) {compress (file, zipout, BaseDir + dir.getname () + file.separator); }}/** * Compressed file * * @param oldFile file to compress * @param zipoUT compressed file stream * @param baseDir baseDir * @throws IOException */private void Compressfile (file oldFile, zipoutputst Ream ZipOut, String baseDir) throws IOException {if (!oldfile.exists ()) {logger.error ("Zipauditlogfile N Ame is {} not exist ", oldFile); Return } Bufferedinputstream bis = null; try {bis = new Bufferedinputstream (new FileInputStream (OldFile)); ZipEntry zipentry = new ZipEntry (BaseDir + oldfile.getname ()); Zipout.putnextentry (ZipEntry); int count; byte data[] = new Byte[zip_buffer]; while ((count = bis.read (data, 0, zip_buffer))! =-1) {zipout.write (data, 0, count); }} finally {if (null! = bis) {bis.close (); } } }}
Java Compression results
Modify permissions can also take advantage of JAVA7 in nio.2 to the metadata file operation support, in particular, can see the use of NIO package, its related tutorials are described at the end of this article.
The code is as follows:
Package Com.website.common;import Java.io.ioexception;import Java.nio.file.filesystems;import java.nio.file.Files; Import Java.nio.file.path;import Java.nio.file.attribute.posixfilepermission;import Java.nio.file.attribute.posixfilepermissions;import java.util.set;/*** provides the file path public function to change the permissions, determine whether the normal file, determine whether the path in the secure path under the * * program:website* @description: Path tool, Modify Permissions * @author: smallsoup* @create: 2018-08-14 07:56**/public class Pathutil {/** * POSIX represents a portable operating system interface and is not limited to UNIX class systems */private static final Boolean isposix = Filesystems.getdefault (). supportedfileat Tributeviews (). Contains ("POSIX"); /** * Digital permission format, such as */private static final int perm_len_three = 3; /** * such as 765 rwxrw_r_x */private static final int perm_len_nine = 9; /** * Set file permissions to be valid under POSIX * * @param file Files * @param perm permissions are similar to "rw-r-----", "640" * @return True Modify success Fals E modification Failed * @throws IOException */public static Boolean setfilepermision (Path file, String Perm) throws IOException {iF (! Isposix) {return true; }//"Rwxr-x---" if (perm.length () = = Perm_len_three) {perm = Trans2strperm (perm); } if (Perm.length ()! = Perm_len_nine) {return false; } set<posixfilepermission> perms = posixfilepermissions.fromstring (perm); Files.setposixfilepermissions (file, perms); return true; }/** * Convert * * @param digitperm A numeric string of length 3 * @return */private static string Trans2strperm (String digi tperm) {StringBuilder builder = new StringBuilder (9); Owner Builder.append (tostringperm (Digitperm.charat (0)); Group Builder.append (Tostringperm (Digitperm.charat (1))); Other Builder.append (Tostringperm (Digitperm.charat (2))); return builder.tostring (); } private static String tostringperm (char ch) {switch (CH-' 0 ') {case 7:return "rwx"; Case 6:return "rw-"; Case 5:return "R-x"; Case 4:return "r--"; Case 3:return "-WX"; Case 2:return "-w-"; Case 1:return "--x"; Case 0:return "---"; Default:return ""; } }}
Go language, NIO and other learning materials can follow the text at the end of the public after the back in the background "1" access.
Finally, CSDN resources, collected a huge amount of learning materials, if you are ready into it pit, inspirational to become a good program ape, then these resources are suitable for you, including Java, go, Python, springcloud, elk, embedded, big data, interview materials, front-end and other resources. At the same time we formed a technical exchange group, there are many big guys, will not regularly share technical articles, if you want to learn to improve together, you can pay attention to the following public number after the reply "2", access.
I am a small bowl of soup, we study together, sweep code attention, wonderful content the first time to push you
Welcome to Scan Code attention