Java 常用的工具接口包括文件压缩,解压缩,IP转换,文件删除(解决中文问题)

    技术2022-06-11  110

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Enumeration; import org.apache.log4j.Logger; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; import org.apache.tools.zip.ZipOutputStream; import com.captech.lang.ErrorType; import com.captech.ws.rs.WebApplicationException; public class CommonUtils { protected static Logger logger = Logger.getLogger(CommonUtils.class); public static long getStringIpToLong(String ip) { ip = ip.trim(); String[] ips = ip.split("[.]"); long num = 16777216L * Long.parseLong(ips[0]) + 65536L * Long.parseLong(ips[1]) + 256 * Long.parseLong(ips[2]) + Long.parseLong(ips[3]); return num; } public static String longToIP(long longIp) { StringBuffer sb = new StringBuffer(""); sb.append(String.valueOf((longIp >>> 24))); sb.append("."); sb.append(String.valueOf((longIp & 0x00FFFFFF) >>> 16)); sb.append("."); sb.append(String.valueOf((longIp & 0x0000FFFF) >>> 8)); sb.append("."); sb.append(String.valueOf((longIp & 0x000000FF))); return sb.toString(); } public static File zipFiles (String filePath, String zipname){ FileOutputStream fos=null; ZipOutputStream zipOut=null; File fileList = new File(filePath); if(fileList.exists() && fileList.isDirectory()){ File[] files=fileList.listFiles(); try { File out = new File(zipname); if(!out.exists()){ out.createNewFile(); } fos = new FileOutputStream(out);//创建文件输出流(低级流) zipOut = new ZipOutputStream(fos);//创建zip文件输出流 zipOut.setEncoding("gbk"); int i = 0; for (i = 0; i < files.length; i++) { writeFile(files[i], zipOut, ""); } zipOut.close(); return out; }catch(IOException e){ throw createAndLogException(ErrorType.FILE_WRITE_ERROR, e); } }else{ throw createAndLogException(ErrorType.FILE_OPEN_ERROR, new Exception("file Not exist")); } } public static void writeFile(File file, ZipOutputStream zipOut, String base){ try { if(file.isDirectory()){ File[] files = file.listFiles(); base = base + file.getName() + File.separator; zipOut.putNextEntry(new ZipEntry(base)); for (int i = 0; i < files.length; i++) { writeFile(files[i], zipOut, base); } }else{ base = base + file.getName(); BufferedReader in=new BufferedReader(new InputStreamReader(new FileInputStream(file),"ISO8859_1")); ZipEntry ze = new ZipEntry(base); zipOut.putNextEntry(ze); int c = 0; while ((c = in.read()) != -1) { zipOut.write(c); } in.close(); } } catch(Exception e){ throw createAndLogException(ErrorType.FILE_WRITE_ERROR, e); } } //删除文件夹的接口 public static boolean deleteDirectory(File file){ if( file.exists() ) { File[] files = file.listFiles(); for(int i=0; i<files.length; i++) { if(files[i].isDirectory()) { deleteDirectory(files[i]); } else { files[i].delete(); } } } return( file.delete()); } //复制文件的接口 public static void copyFile(File sourceFile,File targetFile) { // 新建文件输入流并对它进行缓冲 try { if(!targetFile.getParentFile().exists()){ targetFile.getParentFile().mkdir(); } if(!targetFile.exists()){ targetFile.createNewFile(); } FileInputStream input = new FileInputStream(sourceFile); BufferedInputStream inBuff=new BufferedInputStream(input); // 新建文件输出流并对它进行缓冲 FileOutputStream output = new FileOutputStream(targetFile); BufferedOutputStream outBuff=new BufferedOutputStream(output); // 缓冲数组 byte[] b = new byte[1024 * 5]; int len; while ((len =inBuff.read(b)) != -1) { outBuff.write(b, 0, len); } // 刷新此缓冲的输出流 outBuff.flush(); //关闭流 inBuff.close(); outBuff.close(); output.close(); input.close(); } catch (Exception e) { throw createAndLogException(ErrorType.FILE_WRITE_ERROR, e); } } @SuppressWarnings("unchecked") public static void unzipFile(String unzipfile, String outputDirectory){ try { ZipFile zipFile = new ZipFile(unzipfile,"GBK"); ZipEntry entry; // 创建文件夹 Enumeration<ZipEntry> e = zipFile.getEntries(); while (e.hasMoreElements()) { entry = e.nextElement(); String name = entry.getName(); if (!name.endsWith(File.separator)) { File f = new File(outputDirectory + File.separator + entry.getName()); if (!f.getParentFile().exists()) { f.getParentFile().mkdirs(); } InputStream in = zipFile.getInputStream(entry); FileOutputStream out = new FileOutputStream(f); int c; byte[] by = new byte[1024]; while ((c = in.read(by)) != -1) { out.write(by, 0, c); } out.close(); in.close(); } } zipFile.close(); } catch (IOException e) { createAndLogException(ErrorType.FILE_OPEN_ERROR, new Throwable("unzip file error")); } } 

    注意一点的是,文件压缩与解压缩是用的apache ant的工具类,

    其maven依赖为:

    <dependency> <groupId>org.apache.ant</groupId> <artifactId>ant</artifactId> <version>1.7.0</version> </dependency> 


    最新回复(0)