栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

纯JAVA代码实现压缩文件夹与屏幕截图

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

纯JAVA代码实现压缩文件夹与屏幕截图

文章目录
  • 纯JAVA代码实现压缩文件夹与屏幕截图
    • 压缩文件夹
    • 屏幕截图

纯JAVA代码实现压缩文件夹与屏幕截图 压缩文件夹

使用的java.util.zip里的ZipEntry和ZipOutputStream

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

public class FileZip {
    

    public static void ZipCompress(String inputFile, String outputFile) throws Exception {
        //创建zip输出流
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputFile));
        //创建缓冲输出流
        BufferedOutputStream bos = new BufferedOutputStream(out);
        File input = new File(inputFile);
        System.out.println("压缩开始==》");
//        long begin = System.currentTimeMillis();
        compress(out, bos, input, null);
        bos.close();
        out.close();
        long end = System.currentTimeMillis();
//        System.out.println("文件下载耗时:" + (end - begin) / 1000 + "s");
        System.out.println("==》压缩结束");

    }

    
    //递归压缩
    public static void compress(ZipOutputStream out, BufferedOutputStream bos, File input, String name) throws IOException {

        if (name == null) {
            name = input.getName();
        }
        //如果路径为目录(文件夹)
        if (input.isDirectory()) {
            //取出文件夹中的文件(或子文件夹)
            File[] flist = input.listFiles();
            if (flist.length == 0) {
                //如果文件夹为空,则只需在目的地zip文件中写入一个目录进入
                out.putNextEntry(new ZipEntry(name + "/"));
            } else {
                //如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩
                for (int i = 0; i < flist.length; i++) {
                    compress(out, bos, flist[i], name + "/" + flist[i].getName());
                }
            }
        } else {
            //如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入zip文件中
            out.putNextEntry(new ZipEntry(name));
            //创建文件输入流
            FileInputStream fos = new FileInputStream(input);
            //创建缓冲输入流
            BufferedInputStream bis = new BufferedInputStream(fos);
            int len;
            //将源文件写入到zip文件中
            byte[] buf = new byte[1024];
            while ((len = bis.read(buf)) != -1) {
                bos.write(buf, 0, len);
            }
            bis.close();
            fos.close();
        }
    }
}
屏幕截图

使用的Robot,处于应急情况下使用的,技术含量不高,不是最优的做法,后期学习了其他技术在进行修改。此种方法的话,可以实现对网页的一个截图。

import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import javax.imageio.ImageIO;
public class CutPicture {
    public static void CutImage(String url) throws Exception {

        Desktop.getDesktop().browse(new URL(url).toURI());
        Robot robot = new Robot();
        robot.delay(10000);
        Dimension d = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
        int width = (int) d.getWidth();
        int height = (int) d.getHeight();
        // 最大化浏览器
        robot.keyRelease(KeyEvent.VK_F11);
        robot.delay(100);
        Image image = robot.createScreenCapture(new Rectangle(0, 0, width, height));
        BufferedImage bi = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        Graphics g = bi.createGraphics();
        g.drawImage(image, 0, 0, width, height, null);
        // 保存图片
        ImageIO.write(bi, "jpg", new File("E:\20211109\hello.JPG"));
    }
}

另外一种方式则是:
只能实现屏幕截图

public class Img {

    static String serialName = "word_index";
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    private String fileName; //文件前缀
    private String defaultName = "word";

    private String imageFormat; //图像文件的格式
    private String defaultImageFormat = "png";


    public Img() {
        fileName = defaultName;
        imageFormat = defaultImageFormat;
    }

    public Img(String name, String format) {

        fileName = name;
        imageFormat = format;
    }

    public void snapShot() throws AWTException, IOException, URISyntaxException {
        Desktop.getDesktop().browse(new URL("https://www.baidu.com/").toURI());
        Robot robot = new Robot();
        robot.delay(5000);
        try {
            //拷贝屏幕到一个BufferedImage对象screenshot
            BufferedImage screenshot = (new Robot()).createScreenCapture(new
                Rectangle(0, 0, (int) d.getWidth(), (int) d.getHeight()));
            //根据文件前缀变量和文件格式变量,自动生成文件名
            String name = fileName + serialName + "." + imageFormat;
            File f = new File(name);
            System.out.print("Save File " + name);
            //将screenshot对象写入图像文件
            ImageIO.write(screenshot, imageFormat, f);
            System.out.print("..Finished!n");
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }

    public static void main(String[] args) throws IOException, URISyntaxException, AWTException {
        Img cam = new Img("I:\202611784\", "png");
        cam.snapShot();
    }

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/459976.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号