温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java如何实现文件的归档和解档

发布时间:2021-04-15 10:25:27 来源:亿速云 阅读:140 作者:小新 栏目:编程语言

这篇文章将为大家详细讲解有关Java如何实现文件的归档和解档,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

具体内容如下

文件的归档

package cn.yimen.archiver;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * 归档器
 */
public class Archiver {
 public static void main(String[] args) throws Exception {
 //
 FileOutputStream fos = new FileOutputStream("d:/arch/x.xar");
 fos.write(addFile("D:/arch/a.xls"));
 fos.write(addFile("D:/arch/b.xml"));
 fos.write(addFile("D:/arch/c.jpg"));
 fos.close();
 //

 }

 /**
 * path : d:/xxx/xxx/a.jpg
 * @throws Exception
 */
 public static byte[] addFile(String path) throws Exception{
 //文件
 File f = new File(path);
 //文件名
 String fname = f.getName();
 //文件名数组
 byte[] fnameBytes = fname.getBytes() ;
 //文件内容长度
 int len = (int)f.length();

 //计算总长度  文件名长度 + 文件名内容 + 文件内容长度 + 文件内容
 int total = 4 + fnameBytes.length + 4 + len ;

 //初始化总数组
 byte[] bytes = new byte[total];

 //1.写入文件名长度
 byte[] fnameLenArr = Util.int2Bytes(fnameBytes.length);
 System.arraycopy(fnameLenArr, 0, bytes, 0, 4);

 //2.写入文件名本身
 System.arraycopy(fnameBytes, 0, bytes, 4, fnameBytes.length);

 //3.写入文件内容长度
 byte[] fcontentLenArr = Util.int2Bytes(len);
 System.arraycopy(fcontentLenArr, 0, bytes, 4 + fnameBytes.length, 4);

 //4.写入文件内容
 //读取文件内容到数组中
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 FileInputStream fis = new FileInputStream(f);
 byte[] buf = new byte[1024];
 int len0 = 0 ;
 while((len0 = fis.read(buf)) != -1){
  baos.write(buf, 0, len0);
 }
 fis.close();
 //得到文件内容
 byte[] fileContentArr = baos.toByteArray();

 System.arraycopy(fileContentArr, 0, bytes, 4 + fnameBytes.length + 4, fileContentArr.length);

 return bytes ;
 }
}

文件的解档

package cn.yimen.archiver;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 解档程序
 */
public class Unarchiver {
 public static void main(String[] args) throws Exception {

 List<FileBean> files = new ArrayList<FileBean>();
 //
 FileInputStream fis = new FileInputStream("d:/arch/x.xar");

 FileBean fileBean = null ;
 //
 while((fileBean = readNextFile(fis)) != null){
  files.add(fileBean);
 }
 //关闭流
 fis.close();

 FileOutputStream fos = null ;
 //
 for(FileBean fb : files){
  fos = new FileOutputStream("d:/arch/unarch/" + fb.getFileName());
  fos.write(fb.getFileContent());
  fos.close();
 }
 }

 /**
 * 从流中读取下一个文件
 */
 public static FileBean readNextFile(FileInputStream fis) throws Exception{
 //
 byte[] bytes4 = new byte[4];
 //读取四个字节
 int res = fis.read(bytes4);
 if(res == -1){
  return null ;
 }
 //文件名长度
 int fnameLen = Util.bytes2Int(bytes4);

 //文件名数组
 byte[] fileNameBytes = new byte[fnameLen];
 fis.read(fileNameBytes);

 //得到文件名
 String fname = new String(fileNameBytes);

 //再读取4个字节,作为文件内容的长度
 fis.read(bytes4);
 int fileContLen = Util.bytes2Int(bytes4);

 //读取文件内容
 byte[] fileContBytes = new byte[fileContLen];
 fis.read(fileContBytes);
 return new FileBean(fname,fileContBytes);
 }
}
package cn.yimen.archiver;
/**
 * 文件Bean
 */
public class FileBean {
 private String fileName;
 private byte[] fileContent;

 public FileBean() {
 }

 public FileBean(String fname, byte[] fileContBytes) {
 this.fileName = fname ;
 this.fileContent = fileContBytes ;
 }

 public String getFileName() {
 return fileName;
 }

 public void setFileName(String fileName) {
 this.fileName = fileName;
 }

 public byte[] getFileContent() {
 return fileContent;
 }

 public void setFileContent(byte[] fileContent) {
 this.fileContent = fileContent;
 }

}

工具类

package cn.yimen.archiver;
public class Util {
 /**
 * 整型转换成字节数组
 */
 public static byte[] int2Bytes(int i){
 byte[] arr = new byte[4] ;
 arr[0] = (byte)i ;
 arr[1] = (byte)(i >> 8) ;
 arr[2] = (byte)(i >> 16) ;
 arr[3] = (byte)(i >> 24) ;
 return arr ;
 }

 /**
 * 字节数组转成int
 */
 public static int bytes2Int(byte[] bytes){
 int i0= bytes[0];
 int i1 = (bytes[1] & 0xFF) << 8 ;
 int i2 = (bytes[2] & 0xFF) << 16 ;
 int i3 = (bytes[3] & 0xFF) << 24 ;
 return i0 | i1 | i2 | i3 ;
 }
}

关于“Java如何实现文件的归档和解档”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI