zip与流+JAVA7+Files

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;


public class Test
{


private static Path path;
private static String fileName;
private static File file;


public static void main(String args[])
{
try
{
downFile();
zipExtractor(path);
}
catch (Exception e)
{
e.printStackTrace();
}


}


private static void downFile() throws Exception
{


URL url = new URL(
"http://121.41.108.144:8080/download/130636083243513137/down07.zip");
fileName = url.getFile();
file = new File(fileName);// down07.zip
fileName = file.getName();
path = Paths.get("C:", fileName);// c:\\down07.zip
file = path.toFile();


URLConnection connection = null;


connection = url.openConnection();
int length = connection.getContentLength();
if (file.exists())
{
System.out.println("zzzz");
}
else
{
OutputStream out = new FileOutputStream(file);
copy(connection.getInputStream(), out, length);
out.close();
}
}


private static void copy(InputStream inputStream, OutputStream out,
int length)
{
byte[] buffer = new byte[length];
BufferedInputStream bi = new BufferedInputStream(inputStream);
BufferedOutputStream bo = new BufferedOutputStream(out);
int count = 0, n = 0;
try
{
while ((n = bi.read(buffer, 0, length)) != -1)
{
out.write(buffer, 0, n);
count += n;
}
out.flush();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
bi.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}


private static boolean zipExtractor(Path path) throws Exception
{
boolean isSuccess = false;
Path newPath = Paths.get("C:", "Users", "zzzzz", "Desktop", "new");
File f = newPath.toFile();
if (f.exists() == false)
{
f.mkdirs();
}
path = Paths.get("C:", "Users", "zzzzz", "Desktop", "down07.zip");
ZipFile zipFile = new ZipFile(path.toString());
ZipInputStream zipInputStream = new ZipInputStream(
Files.newInputStream(path, StandardOpenOption.READ));
ZipEntry entry;
FileSystem fs = FileSystems.newFileSystem(path, null);

Enumeration enumeration = zipFile.entries();

while(enumeration.hasMoreElements())
{
entry =(ZipEntry)enumeration.nextElement();
String sourName = entry.getName();
if(newPath.resolve(sourName).toFile().exists() == false)
{
Files.copy(fs.getPath(sourName), newPath.resolve(sourName));
}
else
{
System.out.println("exist");
}
}

// while ((entry = zipInputStream.getNextEntry()) != null)
// {
// String sourceName = entry.getName();
// if (newPath.resolve(entry.getName()).toFile().exists() == false)
// {
// Files.copy(fs.getPath(sourceName),
// newPath.resolve(entry.getName()));
// }
// else
// {
// System.out.println("exist");
// }
// }




return isSuccess;
}


}


代码比较乱

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值