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;
}
}
代码比较乱