zip的文件传输压缩,本质是写在流中,然后流传输,但是不同的文件类型结构不一,要用这种文件的专门的写流,和解析流的工具去处理,这样
得出的流才具备结构,才可用这种结构的解析工具解析
生产文件本系统是 win系统默认是GBK (解决见附件或文章:String与byte[]字节数组中文转换乱码问题
) 文件中有中文会乱码 linux就不会
zip这种复杂结构的文件,是将流写到这种类型的文件对象中,这种zip的流也不会直接转化过来,需要遍历成员,将成员一个一个遍成流,可立即用,可累计
普通的图片等但类型文件,直接变成流即可
方式一:
@Test
public void zip() {
File file = new File("C:\\Users\\yy\\tst\\20271104.zip");
byte[] b = null;
try {
OutputStream oo = new FileOutputStream(new File("C:\\Users\\yy\\tst\\20271109.zip"));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipInputStream zin= new ZipInputStream(new FileInputStream(file));
ZipOutputStream zip = new ZipOutputStream(oo);
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null) {
ZipEntry cacheEntry = new ZipEntry(entry.getName());
if(entry.getSize()==-1l){
cacheEntry.setSize(0l);
}else{
cacheEntry.setSize(entry.getSize());
}
zip.putNextEntry(cacheEntry);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int num;
while ((num = zin.read(buf, 0, buf.length)) != -1) {
baos.write(buf, 0, num);
}
String s = new String(baos.toByteArray(), "UTF-8");
System.out.println(s);
zip.write(baos.toByteArray());//zip流生成zip
zip.closeEntry();
}
// b = bos.toByteArray(); 传递zip流
// process(b);
zip.close();
bos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void process (byte[] a) {
try (ByteArrayInputStream bis = new ByteArrayInputStream(a);
ZipInputStream zip = new ZipInputStream(bis)) {
ZipEntry entry;
while ((entry = zip.getNextEntry()) != null) {
String fileName = entry.getName();
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
byte[] buf = new byte[1024];
int num;
while ((num = zip.read(buf, 0, buf.length)) != -1) {
baos.write(buf, 0, num);
}
String s = new String(baos.toByteArray(), "UTF-8");
System.out.println(s);
// processText(baos.toByteArray(), 0, fileName);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
方式二:
paramzip=sftp.getFileStringZip(loccaldirect+"20271019"+".zip");
===把zip中所有的文件一个放在map中
public Map
//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
File imgFile = new File(path);
Map
try {
ZipInputStream zip = new ZipInputStream(new FileInputStream(imgFile));
ZipEntry zipEntry=null;
while((zipEntry=zip.getNextEntry())!=null){//通过zip.getNextEntry()来得到ZipEntry对象。
String fileName_zip=zipEntry.getName();//得到文件名称
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
byte[] byte_s=new byte[1024];
int num=-1;
while((num=zip.read(byte_s,0,byte_s.length))>-1){//通过read方法来读取文件内容
byteArrayOutputStream.write(byte_s,0,num);
}
byte[] byte_s_=byteArrayOutputStream.toByteArray();
String fileNeirong=new String(byte_s,"UTF-8");//将字节数组转化为字符串,UTF-8格式(容许中文)
// s.replaceAll("[\u0000]", "");
mapparam.put(fileName_zip,fileNeirong.replaceAll("[\u0000]", ""));
System.out.println(fileNeirong);
}}catch (Exception e){
e.printStackTrace();
}
return mapparam;
}
====遍历map中所有的文件 文本就可读,其他就是下载
public static void readTxt(JSONObject myJson) throws IOException{
//1.读取文件
// File file = new File("d:/new4.txt");
// InputStreamReader read = new InputStreamReader(new FileInputStream(file),"utf-8");//考虑到编码格式
// BufferedReader bu = new BufferedReader(read);
// //2.拼接字符串
String lineText = null;
// String insert = "INSERT INTO [report].[dbo].[process] ([bgid]) VALUES ('";
// String insert2 = "');";
for (Map.Entry
System.out.println(entry.getKey() + ":" + entry.getValue());
String key = entry.getKey();
String value = entry.getValue()+"";
Reader reader = new StringReader(value);
BufferedReader bu = new BufferedReader(reader);
//3.边读边写
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(new File("C:\\Users\\yy\\tst1\\"+key)));
BufferedWriter wr = new BufferedWriter(out);
while((lineText = bu.readLine()) != null){
System.out.println(lineText);
wr.write(lineText+"\r\n");
}
reader.close();
wr.close();
}
}
注意工具类中用byteArrayOutputStream转成字节码,不能用byte_s,byte_s只是缓存块,byteArrayOutputStream是累计值
while((num=zip.read(byte_s,0,byte_s.length))>-1){//通过read方法来读取文件内容
byteArrayOutputStream.write(byte_s,0,num);
}
byte[] byte_s_=byteArrayOutputStream.toByteArray();
String fileNeirong1=new String(byte_s_,"UTF-8");
对于流无论read,write操作的对象都是左边的对象(参数中的都是缓存区)
public Map
//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
File imgFile = new File(path);
Map
try {
ZipInputStream zip = new ZipInputStream(new FileInputStream(imgFile));
ZipEntry zipEntry=null;
while((zipEntry=zip.getNextEntry())!=null){//通过zip.getNextEntry()来得到ZipEntry对象。
String fileName_zip=zipEntry.getName();//得到文件名称
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
byte[] byte_s=new byte[1024];
int num=-1;
while((num=zip.read(byte_s,0,byte_s.length))>-1){//通过read方法来读取文件内容
byteArrayOutputStream.write(byte_s,0,num);
}
byte[] byte_s_=byteArrayOutputStream.toByteArray();
// String fileNeirong=new String(byte_s,"GBK");//将字节数组转化为字符串,UTF-8格式(容许中文)
String fileNeirong=new String(byte_s,"UTF-8");//将字节数组转化为字符串,UTF-8格式(容许中文)
String fileNeirong1=new String(byte_s_,"UTF-8");
// String fileNeirong1 = new String(fileNeirong.getBytes("GBK"),"ISO8859-1");
// s.replaceAll("[\u0000]", "");
mapparam.put(fileName_zip,fileNeirong1.replaceAll("[\u0000]", ""));
// System.out.println("byte_s========"+fileNeirong);
System.out.println("byteArrayOutputStream======"+fileNeirong1);
// System.out.println(fileNeirong1);
}}catch (Exception e){
e.printStackTrace();
}
return mapparam;
}