ftp 流压缩写入zip文件

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import com.jcraft.jsch.ChannelSftp;

public class Iotest {

public static void main(String[] args) throws IOException {
try {
testZip();
}catch(Exception e) {
e.printStackTrace();
}

}

public static void testZip() throws Exception {
// FileOutputStream fos = new FileOutputStream("D:\\back\\MotionProSetup_win64\\zhaotest.zip") ;
/* FTPClient ftpClient = new FTPClient();
ftpClient.connect("139.196.174.174", 22);
ftpClient.login("root", "zhao1234.");
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
}
ftpClient.changeWorkingDirectory("/opt/iotest/");*/
// ZipOutputStream zipOut = null;
// zipOut = new ZipOutputStream(ftpClient.storeFileStream("a.zip"));
NasUtils client = new NasUtils();
ChannelSftp sftp1 = client.connect("139.196.174.174", 22, "root","zhao1234.");
ZipOutputStream zos= new ZipOutputStream(sftp1.put("/opt/iotest/zhaotest1.zip"));
InputStream inputStream = sftp1.get("/mnt/test/MotionProSetup_win64/application.ico");
zos.putNextEntry(new ZipEntry("connected1.ico"));


byte[] b=new byte[1024];
int hasRead=0;
int count = 0;
//循环从输入流中取出数据
while((hasRead=inputStream.read(b))>0){
//每读取一次,即写入文件输入流,读了多少,就写多少。
zos.write(b,0,hasRead);
System.out.println("--hasRead---"+hasRead);
count++;
System.out.println("--count---"+count);

}
zos.closeEntry();
Vector vector = client.listFiles("/mnt/test/MotionProSetup_win64",sftp1);
Iterator iterator = vector.iterator();
while (iterator.hasNext()) {
ChannelSftp.LsEntry file = (ChannelSftp.LsEntry) iterator.next();
//文件名称
String fileName = file.getFilename();

if(fileName.length()>3 ) {
//todo 这里可使用if或正则不下载一些文件
// InputStream inputStream = sftp1.get("/mnt/test/MotionProSetup_win64/" + fileName);

FileInputStream inputStream1 = new FileInputStream("D:\\back\\MotionProSetup_win64\\MotionProSetup_win64\\"+fileName);
if (inputStream != null) {
zos.putNextEntry(new ZipEntry(fileName));
write(inputStream, zos);
zos.closeEntry();
}
}
}
zos.close();



/*
//写入一个条目,我们需要给这个条目起个名字,相当于起一个文件名称
zos.putNextEntry(new ZipEntry("application1.ico"));
//往这个条目中写入一定的数据
write("application.ico", zos);
// zos.write("尹正杰".getBytes());
//关闭条目
zos.closeEntry();
zos.putNextEntry(new ZipEntry("connected1.ico"));
write("connected.ico", zos);
zos.closeEntry();

zos.putNextEntry(new ZipEntry("disconnected.ico"));
write("disconnected.ico", zos);
zos.closeEntry();

zos.putNextEntry(new ZipEntry("license.txt"));
write("license.txt", zos);
zos.closeEntry();

zos.putNextEntry(new ZipEntry("logo.bmp"));
write("logo.bmp", zos);
zos.closeEntry();

zos.putNextEntry(new ZipEntry("motionpro_templete.ini"));
write("motionpro_templete.ini", zos);
zos.closeEntry();

zos.putNextEntry(new ZipEntry("MotionProSetup_x64.exe"));
write("MotionProSetup_x64.exe", zos);
zos.closeEntry();


zos.putNextEntry(new ZipEntry("OEM.ini"));
write("OEM.ini", zos);
zos.closeEntry();

zos.putNextEntry(new ZipEntry("OEM_Help.pdf"));
write("OEM_Help.pdf", zos);
zos.closeEntry();

zos.putNextEntry(new ZipEntry("reconnecting.ico"));
write("reconnecting.ico", zos);
zos.closeEntry();*/
// zos.close();
}

public static void testUnzip() throws Exception {
FileInputStream fis = new FileInputStream("D:\\back\\MotionProSetup_win64\\zhaotest.zip");
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry entry = null ;
byte[] buf = new byte[1024] ;
int len = 0 ;
//此处我们需要判断是否zis流中存在条目,你可以理解为是否存在文件内容
while((entry = zis.getNextEntry()) != null){
//此处我们获取条目名称
String name = entry.getName();
System.out.println(name);
//我们将每一个条目进行解压,我们需要指定一个输出路径
FileOutputStream fos = new FileOutputStream("D:\\back\\MotionProSetup_win64\\unzip\\"+ name) ;
while((len = zis.read(buf)) != -1){
fos.write(buf , 0 , len);
}
fos.close();
}
zis.close();
fis.close();
}


public static void write(InputStream fileInputStream, ZipOutputStream zos) {
try {
DataInputStream in = new DataInputStream(new BufferedInputStream(fileInputStream ));
byte[] b=new byte[1024];
int hasRead=0;
int count = 0;
//循环从输入流中取出数据
while((hasRead=in.read(b))>0){
//每读取一次,即写入文件输入流,读了多少,就写多少。
zos.write(b,0,hasRead);
System.out.println("--hasRead---"+hasRead);
count++;
System.out.println("--count---"+count);

}
in.close();
}catch(Exception e) {
e.printStackTrace();
}
}


public static void main1(String[] args) throws IOException {
try {
// 节点流FileOutputStream直接以A.txt作为数据源操作
FileOutputStream fileOutputStream = new FileOutputStream("D:/back/ccbsetup123.exe");
// 过滤流BufferedOutputStream进一步装饰节点流,提供缓冲写
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
fileOutputStream);
// 过滤流DataOutputStream进一步装饰过滤流,使其提供基本数据类型的写
DataOutputStream out = new DataOutputStream(bufferedOutputStream);
// 此处输入节点流,过滤流正好跟上边输出对应,读者可举一反三
DataInputStream in = new DataInputStream(new BufferedInputStream(
new FileInputStream("D:/back/ccbsetup.exe")));
byte[] b=new byte[1024];
int hasRead=0;
int count = 0;

//循环从输入流中取出数据
while((hasRead=in.read(b))>0){
//每读取一次,即写入文件输入流,读了多少,就写多少。
out.write(b,0,hasRead);
System.out.println("--hasRead---"+hasRead);
count++;
System.out.println("--count---"+count);

}
in.close();
out.close();
}catch (IOException e){
e.printStackTrace();
}finally {

}




}

 

}

上一篇:一些DOS命令


下一篇:Submit report 很实用FM:RS_REFRESH_FROM_SELECTOPTIONS