Java实现:本地文件(夹) ————> 本地压缩包

package 文件压缩.本地文件压缩到本地;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 本地文件(夹)——————》压缩并保存在本地
 */

public class test1 {
    public static void main(String[] args) {
        compression("D:\\test_zip_c+++++.zip", new File("D:\\c+++++"));

    }

    /**
     * resultFileName 被压缩的文件会变成一个新的文件,也就是结果,这个结果的文件名叫 resultFileName
     * compressedFile 被压缩的文件
     */
    public static void compression(String 压缩后文件的完整地址, File 被压缩的文件的完整地址) {
        /*压缩 D:\c+++++ 这个文件夹成 D:\test_zip_c+++++.zip  文件*/
        System.out.println("正在压缩中、、、");
        try {
            /*压缩文件 输出流 首先:输出之后的文件名是什么,所以要传入一个FileOutputStream(压缩后文件的完整地址,就是例如c:/user/ss/321.zip)*/
            ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(压缩后文件的完整地址));
            /*但是为了提高效率 一般不直接用压缩文件流本身,而是将其封装在BufferedOutputStream里面,所以,out流的写都是buffer在.write,而不是zipoutxxx.write*/
            BufferedOutputStream bos = new BufferedOutputStream(zipOutputStream);
            zip(zipOutputStream, 被压缩的文件的完整地址, 被压缩的文件的完整地址.getName(), bos);
            bos.close();
            zipOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
        System.out.println("压缩完成、、、");

    }

    /*zip压缩*/
    private static void zip(ZipOutputStream zipOutputStream, File 被压缩的文件的完整地址, String 被压缩的文件文件名, BufferedOutputStream bos) throws IOException {

        /*如果是目录*/
        if (被压缩的文件的完整地址.isDirectory()) {
            File[] files = 被压缩的文件的完整地址.listFiles();
            if (files.length == 0) {
                zipOutputStream.putNextEntry(new ZipEntry(被压缩的文件文件名 + "/"));
            }
            for (File file : files) {
                /*递归处理*/
                zip(zipOutputStream, file, 被压缩的文件文件名 + "/" + file.getName(), bos);
            }
        }
        /*如果不是目录*/
        else {
            /*向压缩流中添加一个单元*/
            zipOutputStream.putNextEntry(new ZipEntry(被压缩的文件文件名));
            /*将文件转换成流读入内存*/
            InputStream inputStream = new FileInputStream(被压缩的文件的完整地址);
            /*同样的道理,为了提高速度,一般都是封装在 BufferedInputStream 里面,所以read一般也不是 input.read 而是bufferinput在.read*/
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

            byte[] bytes = new byte[1024];
            int len = -1;
            /*bufferedInputStream.read(bytes)的意思是,一次性读取流中的数据然后放入 byte[1024] 中*/
            while ((len = bufferedInputStream.read(bytes)) != -1) {
                /*写数据也是,从 buyes中拿数据 去写 */
                bos.write(bytes, 0, len);
            }
            bufferedInputStream.close();
        }
    }


}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本文将介绍如何使用World Wind Java SDK解析shape文件,并在地球上绘制出来。 第一步,需要下载Shapefile插件。Shapefile插件是World Wind Java SDK的一个扩展,它提供了对shape文件的支持。下载地址为:http://builds.worldwind.arc.nasa.gov/worldwind-releases/2.1.0/worldwind-shapefile-2.1.0.zip 第二步,将下载的压缩包解压到你的工程目录下,并将其中的worldwind-shapefile.jar文件添加到你的工程中。 第三步,创建一个ShapefileLayer对象,并指定shape文件的路径。例如: ShapefileLayer layer = new ShapefileLayer(); layer.setPath("path/to/your/shapefile.shp"); 第四步,将该layer添加到WorldWindow对象中。例如: worldWindow.getModel().getLayers().add(layer); 现在,你已经成功地在地球上绘制出了shape文件中的几何图形。 完整代码示例: ``` import gov.nasa.worldwind.BasicModel; import gov.nasa.worldwind.awt.WorldWindowGLCanvas; import gov.nasa.worldwind.geom.Position; import gov.nasa.worldwind.layers.RenderableLayer; import gov.nasa.worldwind.layers.ShapefileLayer; import gov.nasa.worldwind.render.*; import gov.nasa.worldwind.util.*; import javax.swing.*; import java.awt.*; import java.io.File; import java.util.*; public class ShapefileExample extends JFrame { public ShapefileExample() { WorldWindowGLCanvas wwd = new WorldWindowGLCanvas(); wwd.setPreferredSize(new Dimension(1000, 800)); this.getContentPane().add(wwd, BorderLayout.CENTER); this.pack(); WWUtil.alignComponent(null, this, AVKey.CENTER); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); // Set up a basic layer containing the shapefile ShapefileLayer layer = new ShapefileLayer(); layer.setPath("path/to/your/shapefile.shp"); // Set up a renderable layer to hold our labels RenderableLayer labelLayer = new RenderableLayer(); // Create a label for each shape in the shapefile for (ShapefileRecord record : layer.getRecords()) { // Get the shape's attributes Iterable<? extends Map.Entry<? extends String, ? extends Object>> attrs = record.getAttributes(); Iterator<? extends Map.Entry<? extends String, ? extends Object>> iterator = attrs.iterator(); String name = ""; while (iterator.hasNext()) { Map.Entry<? extends String, ? extends Object> attr = iterator.next(); if ("name".equalsIgnoreCase(attr.getKey())) { name = attr.getValue().toString(); break; } } // Create a label for the shape Shapefile shape = record.getShape(); if (shape instanceof Polyline) { Polyline polyline = (Polyline) shape; Position position = polyline.getStartPosition(); Polygon polygon = new Polygon(polyline.getPositions()); labelLayer.addRenderable(new SurfacePolyline(polygon, 5)); labelLayer.addRenderable(new SurfaceText(name, position)); } else if (shape instanceof Polygon) { Polygon polygon = (Polygon) shape; Position position = polygon.getCentroid(); labelLayer.addRenderable(new SurfacePolygon(polygon)); labelLayer.addRenderable(new SurfaceText(name, position)); } } // Add the layers to the WorldWindow wwd.getModel().getLayers().add(layer); wwd.getModel().getLayers().add(labelLayer); wwd.getModel().setGlobe(new Earth()); wwd.getModel().setView(new BasicOrbitView()); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new ShapefileExample(); } }); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值