一、前言

基于apache的commons-compress包中的org.apache.commons.compress.archivers.tar.TarUtils打包工具类对文件进行加解密、并对加密包文件解析解析parseOctal/parseBinaryLong/parseBinaryBigInteger/parseName、对文件包字节数据进行格式化formatNameBytes/formatUnsignedOctalString/formatLongBinary/formatBigIntegerBinary/formatCheckSumOctalBytes等操作。

二、源码说明package org.apache.commons.compress.archivers.tar;@b@@b@import java.io.IOException;@b@import java.math.BigInteger;@b@import java.nio.ByteBuffer;@b@import org.apache.commons.compress.archivers.zip.ZipEncoding;@b@import org.apache.commons.compress.archivers.zip.ZipEncodingHelper;@b@@b@public class TarUtils@b@{@b@  private static final int BYTE_MASK = 255;@b@  static final ZipEncoding DEFAULT_ENCODING = ZipEncodingHelper.getZipEncoding(null);@b@  static final ZipEncoding FALLBACK_ENCODING = new ZipEncoding() {@b@    public boolean canEncode(String name) { return true; }@b@@b@    public ByteBuffer encode(String name) {@b@      int length = name.length();@b@      byte[] buf = new byte[length];@b@@b@      for (int i = 0; i  55)) {@b@        throw new IllegalArgumentException(exceptionMessage(buffer, offset, length, start, currentByte));@b@      }@b@@b@      result = (result <= 9) {@b@      throw new IllegalArgumentException("At offset " + offset + ", " + length + " byte binary number" + " exceeds maximum signed long" + " value");@b@    }@b@@b@    long val = 0L;@b@    for (int i = 1; i  63) {@b@      throw new IllegalArgumentException("At offset " + offset + ", " + length + " byte binary number" + " exceeds maximum signed long" + " value");@b@    }@b@@b@    return ((negative) ? -val.longValue() : val.longValue());@b@  }@b@@b@  public static boolean parseBoolean(byte[] buffer, int offset)@b@  {@b@    return (buffer[offset] == 1);@b@  }@b@@b@  private static String exceptionMessage(byte[] buffer, int offset, int length, int current, byte currentByte)@b@  {@b@    String string = new String(buffer, offset, length);@b@    string = string.replaceAll("", "{NUL}");@b@    String s = "Invalid byte " + currentByte + " at offset " + (current - offset) + " in '" + string + "' len=" + length;@b@    return s;@b@  }@b@@b@  public static String parseName(byte[] buffer, int offset, int length)@b@  {@b@    try@b@    {@b@      return parseName(buffer, offset, length, DEFAULT_ENCODING);@b@    } catch (IOException ex) {@b@      try {@b@        return parseName(buffer, offset, length, FALLBACK_ENCODING);@b@      }@b@      catch (IOException ex2) {@b@        throw new RuntimeException(ex2);@b@      }@b@    }@b@  }@b@@b@  public static String parseName(byte[] buffer, int offset, int length, ZipEncoding encoding)@b@    throws IOException@b@  {@b@    int len = length;@b@    for (; len > 0; --len)@b@      if (buffer[(offset + len - 1)] != 0)@b@        break;@b@@b@@b@    if (len > 0) {@b@      byte[] b = new byte[len];@b@      System.arraycopy(buffer, offset, b, 0, len);@b@      return encoding.decode(b);@b@    }@b@    return "";@b@  }@b@@b@  public static int formatNameBytes(String name, byte[] buf, int offset, int length)@b@  {@b@    try@b@    {@b@      return formatNameBytes(name, buf, offset, length, DEFAULT_ENCODING);@b@    } catch (IOException ex) {@b@      try {@b@        return formatNameBytes(name, buf, offset, length, FALLBACK_ENCODING);@b@      }@b@      catch (IOException ex2)@b@      {@b@        throw new RuntimeException(ex2);@b@      }@b@    }@b@  }@b@@b@  public static int formatNameBytes(String name, byte[] buf, int offset, int length, ZipEncoding encoding)@b@    throws IOException@b@  {@b@    int len = name.length();@b@    ByteBuffer b = encoding.encode(name);@b@    while ((b.limit() > length) && (len > 0))@b@      b = encoding.encode(name.substring(0, --len));@b@@b@    int limit = b.limit() - b.position();@b@    System.arraycopy(b.array(), b.arrayOffset(), buf, offset, limit);@b@@b@    for (int i = limit; i = 0) && (val != 0L); --remaining)@b@      {@b@        buffer[(offset + remaining)] = (byte)(48 + (byte)(int)(val & 0x7));@b@        val >>>= 3;@b@      }@b@@b@      if (val != 0L) {@b@        throw new IllegalArgumentException(value + "=" + Long.toOctalString(value) + " will not fit in octal number buffer of length " + length);@b@      }@b@@b@    }@b@@b@    for (; remaining >= 0; --remaining)@b@      buffer[(offset + remaining)] = 48;@b@  }@b@@b@  public static int formatOctalBytes(long value, byte[] buf, int offset, int length)@b@  {@b@    int idx = length - 2;@b@    formatUnsignedOctalString(value, buf, offset, idx);@b@@b@    buf[(offset + idx++)] = 32;@b@    buf[(offset + idx)] = 0;@b@@b@    return (offset + length);@b@  }@b@@b@  public static int formatLongOctalBytes(long value, byte[] buf, int offset, int length)@b@  {@b@    int idx = length - 1;@b@@b@    formatUnsignedOctalString(value, buf, offset, idx);@b@    buf[(offset + idx)] = 32;@b@@b@    return (offset + length);@b@  }@b@@b@  public static int formatLongOctalOrBinaryBytes(long value, byte[] buf, int offset, int length)@b@  {@b@    long maxAsOctalChar = (length == 8) ? 2097151L : 8589934591L;@b@@b@    boolean negative = value = max) {@b@      throw new IllegalArgumentException("Value " + value + " is too large for " + length + " byte field.");@b@    }@b@@b@    if (negative) {@b@      val ^= max - 1L;@b@      val |= 255 <= offset; --i) {@b@      buf[i] = (byte)(int)val;@b@      val >>= 8;@b@    }@b@  }@b@@b@  private static void formatBigIntegerBinary(long value, byte[] buf, int offset, int length, boolean negative)@b@  {@b@    BigInteger val = BigInteger.valueOf(value);@b@    byte[] b = val.toByteArray();@b@    int len = b.length;@b@    int off = offset + length - len;@b@    System.arraycopy(b, 0, buf, off, len);@b@    byte fill = (byte)((negative) ? 255 : 0);@b@    for (int i = offset + 1; i  0)@b@          digits = 6;@b@@b@        b = 32;@b@      }@b@      unsignedSum += (0xFF & b);@b@      signedSum += b;@b@    }@b@@b@    return ((storedSum == unsignedSum) || (storedSum == signedSum) || (storedSum > unsignedSum));@b@  }@b@}

ZipEncoding接口package org.apache.commons.compress.archivers.zip;@b@@b@import java.io.IOException;@b@import java.nio.ByteBuffer;@b@@b@public abstract interface ZipEncoding@b@{@b@  public abstract boolean canEncode(String paramString);@b@@b@  public abstract ByteBuffer encode(String paramString)@b@    throws IOException;@b@@b@  public abstract String decode(byte[] paramArrayOfByte)@b@    throws IOException;@b@}

ZipEncodingHelper类package org.apache.commons.compress.archivers.zip;@b@@b@import java.nio.ByteBuffer;@b@import java.nio.charset.Charset;@b@import java.nio.charset.UnsupportedCharsetException;@b@import java.util.Collections;@b@import java.util.HashMap;@b@import java.util.Map;@b@@b@public abstract class ZipEncodingHelper@b@{@b@  private static final Map simpleEncodings;@b@  private static final byte[] HEX_DIGITS;@b@  static final String UTF8 = "UTF8";@b@  private static final String UTF_DASH_8 = "UTF-8";@b@  static final ZipEncoding UTF8_ZIP_ENCODING;@b@@b@  static ByteBuffer growBuffer(ByteBuffer b, int newCapacity)@b@  {@b@    b.limit(b.position());@b@    b.rewind();@b@@b@    int c2 = b.capacity() * 2;@b@    ByteBuffer on = ByteBuffer.allocate((c2 > '\f' & 0xF)]);@b@    bb.put(HEX_DIGITS[(c >> '\b' & 0xF)]);@b@    bb.put(HEX_DIGITS[(c >> '\4' & 0xF)]);@b@    bb.put(HEX_DIGITS[(c & 0xF)]);@b@  }@b@@b@  public static ZipEncoding getZipEncoding(String name)@b@  {@b@    if (isUTF8(name)) {@b@      return UTF8_ZIP_ENCODING;@b@    }@b@@b@    if (name == null) {@b@      return new FallbackZipEncoding();@b@    }@b@@b@    SimpleEncodingHolder h = (SimpleEncodingHolder)simpleEncodings.get(name);@b@@b@    if (h != null) {@b@      return h.getEncoding();@b@    }@b@@b@    try@b@    {@b@      Charset cs = Charset.forName(name);@b@      return new NioZipEncoding(cs);@b@    } catch (UnsupportedCharsetException e) {@b@    }@b@    return new FallbackZipEncoding(name);@b@  }@b@@b@  static boolean isUTF8(String encoding)@b@  {@b@    if (encoding == null)@b@    {@b@      encoding = System.getProperty("file.encoding");@b@    }@b@    return (("UTF8".equalsIgnoreCase(encoding)) || ("UTF-8".equalsIgnoreCase(encoding)));@b@  }@b@@b@  static@b@  {@b@    Map se = new HashMap();@b@@b@    char[] cp437_high_chars = { 199, 252, 233, 226, 228, 224, 229, 231, 234, 235, 232, 239, 238, 236, 196, 197, 201, 230, 198, 244, 246, 242, 251, 249, 255, 214, 220, 162, 163, 165, 8359, 402, 225, 237, 243, 250, 241, 209, 170, 186, 191, 8976, 172, 189, 188, 161, 171, 187, 9617, 9618, 9619, 9474, 9508, 9569, 9570, 9558, 9557, 9571, 9553, 9559, 9565, 9564, 9563, 9488, 9492, 9524, 9516, 9500, 9472, 9532, 9566, 9567, 9562, 9556, 9577, 9574, 9568, 9552, 9580, 9575, 9576, 9572, 9573, 9561, 9560, 9554, 9555, 9579, 9578, 9496, 9484, 9608, 9604, 9612, 9616, 9600, 945, 223, 915, 960, 931, 963, 181, 964, 934, 920, 937, 948, 8734, 966, 949, 8745, 8801, 177, 8805, 8804, 8992, 8993, 247, 8776, 176, 8729, 183, 8730, 8319, 178, 9632, 160 };@b@@b@    SimpleEncodingHolder cp437 = new SimpleEncodingHolder(cp437_high_chars);@b@@b@    se.put("CP437", cp437);@b@    se.put("Cp437", cp437);@b@    se.put("cp437", cp437);@b@    se.put("IBM437", cp437);@b@    se.put("ibm437", cp437);@b@@b@    char[] cp850_high_chars = { 199, 252, 233, 226, 228, 224, 229, 231, 234, 235, 232, 239, 238, 236, 196, 197, 201, 230, 198, 244, 246, 242, 251, 249, 255, 214, 220, 248, 163, 216, 215, 402, 225, 237, 243, 250, 241, 209, 170, 186, 191, 174, 172, 189, 188, 161, 171, 187, 9617, 9618, 9619, 9474, 9508, 193, 194, 192, 169, 9571, 9553, 9559, 9565, 162, 165, 9488, 9492, 9524, 9516, 9500, 9472, 9532, 227, 195, 9562, 9556, 9577, 9574, 9568, 9552, 9580, 164, 240, 208, 202, 203, 200, 305, 205, 206, 207, 9496, 9484, 9608, 9604, 166, 204, 9600, 211, 223, 212, 210, 245, 213, 181, 254, 222, 218, 219, 217, 253, 221, 175, 180, 173, 177, 8215, 190, 182, 167, 247, 184, 176, 168, 183, 185, 179, 178, 9632, 160 };@b@@b@    SimpleEncodingHolder cp850 = new SimpleEncodingHolder(cp850_high_chars);@b@@b@    se.put("CP850", cp850);@b@    se.put("Cp850", cp850);@b@    se.put("cp850", cp850);@b@    se.put("IBM850", cp850);@b@    se.put("ibm850", cp850);@b@    simpleEncodings = Collections.unmodifiableMap(se);@b@@b@    HEX_DIGITS = new byte[] { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70 };@b@@b@    UTF8_ZIP_ENCODING = new FallbackZipEncoding("UTF8");@b@  }@b@@b@  private static class SimpleEncodingHolder@b@  {@b@    private final char[] highChars;@b@    private Simple8BitZipEncoding encoding;@b@@b@    SimpleEncodingHolder(char[] highChars)@b@    {@b@      this.highChars = highChars;@b@    }@b@@b@    public synchronized Simple8BitZipEncoding getEncoding()@b@    {@b@      if (this.encoding == null)@b@        this.encoding = new Simple8BitZipEncoding(this.highChars);@b@@b@      return this.encoding;@b@    }@b@  }@b@}

java tar压缩工具类_分享apache的commons-compress的TarUtils压缩工具类对文件数据进行加解密、解析及格式化校验等操作...相关推荐

  1. java 线程 基类_关于Linux 系统下 C++ 的多线程基类 Thread

    最近在Linux 下用C++做的东西,原来是使用"多进程"方式实现,现在随着工程的增大,"多进程"的"变量共享"问题已经十分突出了,虽然可以 ...

  2. python定义一个triangle类_设计一个程序,程序中有三个类,Triangle,Lader,Circle。...

    //此程序写出三个类,triangle,lader,circle:其中triangle类具有类型为double的a,b,c边以及周长,面积属性, //具有周长,面积以及修改三边的功能,还有判断能否构成 ...

  3. 前端CryptoJS和Java后端数据互相加解密(AES)

    目录 一.序言 二.关于前端CryptoJS 1.CryptoJS简单介绍 2.加密和填充模式选择 3.前端AES加解密示例 (1) cryptoutils工具类 (2) 测试用例 (3) 加解密后输 ...

  4. Java基于OpenSSL生成 RSA 公私钥对数据进行加解密

    使用OpenSSL来生成私钥和公钥 验证机器上是否安装openssl 命令:openssl version -a [root@node00 test]# openssl version -a Open ...

  5. java万能编码转换_分享万能java字符串编码转换工具类

    package com.zuidaima.util; import java.io.UnsupportedEncodingException; /** * 转换字符串的编码 */ public cla ...

  6. java接口常见问题分析_常见问题 - Apache ServiceComb

    Q: ServiceComb和SpringCloud是什么关系,具体的应用场景是什么? A: ServiceComb是华为基于内部多个大型IT系统实践提炼出来的一套微服务开发框架,在开发态基于最佳实践 ...

  7. java 编译器获得型号_关于编译器构造:如何找到已编译类的目标Java版本?

    重复: Tool to read and display Java .class versions 如果我有一个已编译的Java类,是否可以仅从类文件中得知其目标版本兼容性是什么? 具体来说,我有许多 ...

  8. java+基于dom4j工具类_基于dom4j的xml映射实体的工具类(java)

    基于dom4j的xml映射实体的工具类(java) 工具是基于dom4j,使用反射使实体属性与xml的标签关联,目前兼容List,Map后续更新兼容. 历史更新: 发布-2019-6-22 增加注解@ ...

  9. hadoop创建java项目的步骤_为 Apache Hadoop 创建 Java MapReduce - Azure HDInsight | Microsoft Docs...

    您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn. 为 HDInsight ...

最新文章

  1. R语言KMeans聚类模型示例
  2. UVa202 循环小数
  3. POJ 1160 Post Office
  4. mac下修改mysql默认字符集为utf8
  5. Nginx笔记-处女篇
  6. php 生成css文件怎么打开,php生成html文件的多种步骤介绍
  7. php filespl,PHP SPL--遍历目录
  8. 动画函数,为任意一个元素移动到指定的目标位置
  9. easyUI的使用.datagrid()生成列表日期时间显示异常,一个方法带入有效修复
  10. 磁盘块与扇区的区别和联系
  11. 设计模式---命令模式(C++实现)
  12. 把docx格式的word文档转换为txt文件
  13. JDBC——Java连接关系型数据库
  14. 交叉连接、内连接和外连接的区别及使用方式
  15. Spring Boot 实现通用 Auth 认证的 4 种方式!
  16. 【智能硬件】Jetson nana 扩展显存(内存)
  17. 『牛角书』基于JS实现的鸿蒙游戏——二十四点纸牌
  18. GPU、CPU、显卡区别
  19. 求帮忙解决图灵机器人运行不回复的问题
  20. 自考计算机专业实践好多,自考计算机实践难考吗?

热门文章

  1. LeetCode 101. Symmetric Tree
  2. 洛谷 P1840 【Color the Axis_NOI导刊2011提高(05)】 题解
  3. 【WPF】如何获取命令行参数
  4. 集合源码(一)之hashMap、ArrayList
  5. mysql参数积累 持续更新。。。
  6. 运用C#在采集时进行自动验证登录[转]
  7. Windows Phone 7 Silverlight控件展示(含34个控件)
  8. SQL-Server 语句存档整理
  9. 处理Clob数据(转)关于oracle中大对象处理的一些方法和实例
  10. 人工智能再次参加高考:和作家比写作文,AI能打多少分?