「javanio压缩」Java压缩
今天给各位分享javanio压缩的知识,其中也会对Java压缩进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
java 解压文件
给你找了一个 你参考一下吧:
package com.da.unzip;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class Unzip {
public static void main(String[] args) throws Exception {
Unzip unzip = new Unzip();
String zippath = "C:\\unzip\\";// /解压到的目标文件路径
String zipDir = "C:\\data\\";// 要解压的压缩文件的存放路径
File file = new File(zipDir);
List list = unzip.getSubFiles(file);
for (Object obj : list) {
String realname = ((File)obj).getName();
System.out.println(realname);
int end = realname.lastIndexOf(".");
System.out.println("要解压缩的文件名.........."+zipDir+realname);
System.out.println("解压到的目录" +zippath+realname.substring(0, end));
unzip.testReadZip(zippath,zipDir+realname);
}
}
/*
* 解压缩功能. 将zippath目录文件解压到unzipPath目录下. @throws Exception
*/
public void ReadZip(String zippath, String unzipPath) throws Exception {
ZipFile zfile = new ZipFile(unzipPath);// 生成一个zip文件对象
System.out.println(zfile.getName());// 获取要解压的zip的文件名全路径
Enumeration zList = zfile.entries();// 返回枚举对象
ZipEntry ze = null;// 用于表示 ZIP 文件条目
byte[] buf = new byte[1024];// 声明字节数组
/**
* 循环获取zip文件中的每一个文件
*/
while (zList.hasMoreElements()) {
// 从ZipFile中得到一个ZipEntry
ze = (ZipEntry) zList.nextElement();
if (ze.isDirectory())// 如果为目录条目,则返回 true,执行下列语句
{
System.out.println("Dir: " + ze.getName() + " skipped..");
continue;
}
int begin = zfile.getName().lastIndexOf("\\") + 1;
int end = zfile.getName().lastIndexOf(".");
String zipRealName = zfile.getName().substring(begin, end);
System.out.println("解压缩开始Extracting:"+ze.getName()+"\t"+ze.getSize()+"\t"+ze.getCompressedSize());
// 以ZipEntry为参数得到一个InputStream,并写到OutputStream中,并加上缓冲
OutputStream os = new BufferedOutputStream(
new FileOutputStream(getRealFileName(zippath + "\\"
+ zipRealName, ze.getName())));
InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
String fileName = getRealFileName(zippath, ze.getName()).getName();
System.out.println("解压出的文件名称:" + fileName);
int readLen = 0;
while ((readLen = is.read(buf, 0, 1024)) != -1) {
os.write(buf, 0, readLen);
}
is.close();
os.close();
// System.out.println("解压缩结束Extracted: "+ze.getName());
}
zfile.close();
}
/**
* 给定根目录,返回一个相对路径所对应的实际文件名.
*
* @param zippath
* 指定根目录
* @param absFileName
* 相对路径名,来自于ZipEntry中的name
* @return java.io.File 实际的文件
*/
private File getRealFileName(String zippath, String absFileName) {
String[] dirs = absFileName.split("/", absFileName.length());
File ret = new File(zippath);// 创建文件对象
if (dirs.length 1) {
for (int i = 0; i dirs.length - 1; i++) {
ret = new File(ret, dirs[i]);
}
}
if (!ret.exists()) {// 检测文件是否存在
ret.mkdirs();// 创建此抽象路径名指定的目录
}
ret = new File(ret, dirs[dirs.length - 1]);// 根据 ret 抽象路径名和 child
// 路径名字符串创建一个新 File 实例
return ret;
}
}
java如何实现多个文件的压缩
import java.util.*;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.*;
public class ZipFSPUser {
public static void main(String [] args) throws Throwable {
MapString, String env = new HashMap();
env.put("create", "true");
// locate file system by using the syntax
// defined in java.net.JarURLConnection
URI uri = URI.create("jar:file:/codeSamples/zipfs/zipfstest.zip");
try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
Path externalTxtFile = Paths.get("/codeSamples/zipfs/SomeTextFile.txt");
Path pathInZipfile = zipfs.getPath("/SomeTextFile.txt");
// copy a file into the zip file
Files.copy( externalTxtFile,pathInZipfile,
StandardCopyOption.REPLACE_EXISTING );
}
}
}
创建一个zip文件,并添加一个文件进去,需要JDK7
java NIO编写服务器和客户端可以读写Object?求给个例子
客户端发送:
len+type+command+userid+":"+content+";"
例: 32 001 001 10001 : helloworld!;
其中32为len为整数 001为type为short,001为command为short,
10001为userid为八字节long, content为变长字符串。“;”和":"分别一字节。
type: 判断type可知道客户端的请求类型,如是聊天还是游戏
command: 判断command类型可知道客户端的请求具体命令,如聊天,表情等
所以,整个数据包头长度为: 4+2+2+8+1+content长+1
content的长度可以算出来;
byte[] stringBytes=content.getBytes();
len= StringBytes.length();
数据包的头部长度为: int headLen=4+2+2+8+1;
数据包中数据体的长度为: int datalen= len+1;
整个数据包的长度为: headLen+datalen;
客户端发送时压成 :
byte[] toSend=new byte[headLen+datalen];
分别用System.arrayCopy命令将内容考入soSend中
copy datalen 到toSend的前两个字节
copy type 到toSend 占两个字节
copy command
copy userid
copy ":"
copy content.getBytes()
copy ";"
//行了, tosend中己经有压缩编码好的消息了
outputStream.write(toSend);//发送到服务端
outputStream.flash();//别忘了flash
////////////// 客户端发送完毕
服务端收取
byte[] datas=new byte[len];
inputStream.read(datas);
现在datas中己包含客户端发过来的byte[]
//////////// 下面开始解析
1。从 datas中拿两个字节出来得到len
2。再拿两个字节出来,得到 type
3。得到command
4。得到userid
5。拿一个字节 得到:或者;,如果没有:只有;证明客户端就发了个空消息
6。根据len的长度拿len个字节,得到String str=new String(指定长度拿来的byte[],指定编码);
7。 解析str,丢弃; 细分str的具体内容
//////////// 解析结束
另外一种办法,很偷懒,效率低一丁点:
全部用String 不用计算长度,最简单, 各数据项之间用","分割
String toString="len,type,command,userid: targetUserid,chatContent;"
这种形式最简单,效率会低点,但你初学可以直接用,以后再改进。
具体如下:
StringBuffer buffer =new StringBuffer();
buffer.append(len);
buffer.append(",");
buffer.append(type);
buffer.append(",");
buffer.append(command);
buffer.append(,);
buffer.append(userid);
buffer.append(":");
buffer.append(对方帐号);
buffer.append(",");
buffer.append(内容);
buffer.append(";");
String result=buffer.toString();
outputstream.write(result.getBytes("utf-8"));
flash()
///服务端收到以后,是纯字符串
先按";"split得到数个消息包
再按":"得到消息体得消息头
再按","解析具体内容
javanio压缩的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Java压缩、javanio压缩的信息别忘了在本站进行查找喔。
发布于:2022-12-07,除非注明,否则均为
原创文章,转载请注明出处。