java將查詢數據寫入zip壓縮包內

  public static void filetest() throws IOException {
      String zipPath = "D:\\fileTest\\image\\3.zip";      //壓縮包路徑
      String str1 = "測試test123abc";                      //需要寫入的數據
      String str2 = "測試2";
      String Name1 = StringUtils.join("文件.json");      //壓縮包里的文件
      String Name2 = StringUtils.join("file/文件1.json");  //在壓縮包里創(chuàng)建file目錄下的文件
      //創(chuàng)建壓縮包
      ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipPath));
    //與方式二匹配使用(使用緩沖流)     
      BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(zipOutputStream);
      //創(chuàng)建壓縮包里的文件
      zipOutputStream.putNextEntry(new ZipEntry(Name1));
      byte[] bytes1 = str1.getBytes(StandardCharsets.UTF_8);
      zipOutputStream.write(bytes1, 0, bytes1.length);    //將數據寫入到壓縮包里的文件里面
      zipOutputStream.closeEntry();
 
      zipOutputStream.putNextEntry(new ZipEntry(Name2));
      byte[] bytes2 = str2.getBytes(StandardCharsets.UTF_8);
    //方式一將數據寫入文件(不適用緩沖流)     
    // zipOutputStream.write(bytes2, 0, bytes2.length);
    //方式二將數據寫入文件(使用緩存流)      
      bufferedOutputStream.write(bytes, 0, bytes.length);
 
      zipOutputStream.closeEntry();
 
      zipOutputStream.flush();
      zipOutputStream.close();
  }
image.png
  • f ile文件夾里面是文件1.json胃惜,里面的內容是“測試2”,文件.json的內容則是“測試test123abc”鼎姐。

使用緩沖流將文件寫入json文件


    public static void writeString(File file, String content) {

        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(file));
            bufferedWriter.write(content);
            bufferedWriter.flush();
            bufferedWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //關閉流釋放資源
                bufferedWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
//測試
  public static void main(String[] args) {

        long stime1 = System.currentTimeMillis();
        for (int j = 0; j <100 ; j++) {
            File file = new File("G://wyh//out"+j+".json");
            boolean b = file.exists();
            if (!b) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            List<Map<String, Object>> list = new ArrayList<>();
            int[] ints = new int[]{1,2,34,2,3,2};
            Map<String, Object> map = new HashMap<>();
            Map<String, Object> map1 = new HashMap<>();

            list.add(map);
            for (int i = 0; i < 100000; i++) {
                map.put("Image"+i,"圖片");
                map.put("Width"+i,"800");
                map.put("Height"+i,"Height");
                map.put("Title"+i,"View from 15th Floor");

                map1.put("Image","圖片");
                map1.put("Width","800");
                map1.put("Height","Height");
                map1.put("Title","View from 15th Floor");
                map.put("IDsmap"+i,map1);
                map.put("IDs"+i,ints);
            }


            String json = JSON.toJSONString(map);
            writeString(file, json);
        }
        long etime1 = System.currentTimeMillis();
        System.out.println("-----------end-----------"+(etime1 - stime1));

    }

多文件壓縮

package com.edc.erp.task.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 壓縮多個文件
 */
public class ZipMultiFile {
    public static void main(String[] args) {
        File[] srcFiles = {new File(""), new File("D:\\hhh\\Massage-app.zip")};
        File zipFile = new File("D:\\hhh\\"+System.currentTimeMillis()+".zip");
        // 調用壓縮方法
        zipFiles(srcFiles, zipFile);
        System.out.println("壓縮完成钾麸!");
    }

    public static void zipFiles(File[] srcFiles, File zipFile) {
        // 判斷壓縮后的文件存在不,不存在則創(chuàng)建
        if (!zipFile.exists()) {
            try {
                zipFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 創(chuàng)建 FileOutputStream 對象
        FileOutputStream fileOutputStream = null;
        // 創(chuàng)建 ZipOutputStream
        ZipOutputStream zipOutputStream = null;
        // 創(chuàng)建 FileInputStream 對象
        FileInputStream fileInputStream = null;

        try {
            // 實例化 FileOutputStream 對象
            fileOutputStream = new FileOutputStream(zipFile);
            // 實例化 ZipOutputStream 對象
            zipOutputStream = new ZipOutputStream(fileOutputStream);
            // 創(chuàng)建 ZipEntry 對象
            ZipEntry zipEntry = null;
            // 遍歷源文件數組
            for (int i = 0; i < srcFiles.length; i++) {
                // 將源文件數組中的當前文件讀入 FileInputStream 流中
                fileInputStream = new FileInputStream(srcFiles[i]);
                // 實例化 ZipEntry 對象炕桨,源文件數組中的當前文件
                zipEntry = new ZipEntry(srcFiles[i].getName());
                zipOutputStream.putNextEntry(zipEntry);
                // 該變量記錄每次真正讀的字節(jié)個數
                int len;
                // 定義每次讀取的字節(jié)數組
                byte[] buffer = new byte[1024];
                while ((len = fileInputStream.read(buffer)) > 0) {
                    zipOutputStream.write(buffer, 0, len);
                }
            }
            zipOutputStream.closeEntry();
            zipOutputStream.close();
            fileInputStream.close();
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末饭尝,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子献宫,更是在濱河造成了極大的恐慌钥平,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,430評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件姊途,死亡現場離奇詭異涉瘾,居然都是意外死亡,警方通過查閱死者的電腦和手機捷兰,發(fā)現死者居然都...
    沈念sama閱讀 94,406評論 3 398
  • 文/潘曉璐 我一進店門立叛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人贡茅,你說我怎么就攤上這事囚巴。” “怎么了?”我有些...
    開封第一講書人閱讀 167,834評論 0 360
  • 文/不壞的土叔 我叫張陵彤叉,是天一觀的道長庶柿。 經常有香客問我,道長秽浇,這世上最難降的妖魔是什么浮庐? 我笑而不...
    開封第一講書人閱讀 59,543評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮柬焕,結果婚禮上审残,老公的妹妹穿的比我還像新娘。我一直安慰自己斑举,他們只是感情好搅轿,可當我...
    茶點故事閱讀 68,547評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著富玷,像睡著了一般璧坟。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上赎懦,一...
    開封第一講書人閱讀 52,196評論 1 308
  • 那天雀鹃,我揣著相機與錄音,去河邊找鬼励两。 笑死黎茎,一個胖子當著我的面吹牛,可吹牛的內容都是我干的当悔。 我是一名探鬼主播傅瞻,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼盲憎!你這毒婦竟也來了俭正?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,671評論 0 276
  • 序言:老撾萬榮一對情侶失蹤焙畔,失蹤者是張志新(化名)和其女友劉穎掸读,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體宏多,經...
    沈念sama閱讀 46,221評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡儿惫,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,303評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現自己被綠了伸但。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片肾请。...
    茶點故事閱讀 40,444評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖更胖,靈堂內的尸體忽然破棺而出铛铁,到底是詐尸還是另有隱情隔显,我是刑警寧澤,帶...
    沈念sama閱讀 36,134評論 5 350
  • 正文 年R本政府宣布饵逐,位于F島的核電站括眠,受9級特大地震影響,放射性物質發(fā)生泄漏倍权。R本人自食惡果不足惜掷豺,卻給世界環(huán)境...
    茶點故事閱讀 41,810評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望薄声。 院中可真熱鬧当船,春花似錦、人聲如沸默辨。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,285評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽缩幸。三九已至壹置,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間桌粉,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,399評論 1 272
  • 我被黑心中介騙來泰國打工衙四, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留铃肯,地道東北人。 一個月前我還...
    沈念sama閱讀 48,837評論 3 376
  • 正文 我出身青樓传蹈,卻偏偏與公主長得像押逼,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子惦界,可洞房花燭夜當晚...
    茶點故事閱讀 45,455評論 2 359

推薦閱讀更多精彩內容

  • 最近一朋友正準備跳槽挑格,就從各處搜索整理一些基礎,便于朋友復習沾歪,也便于自己復習查看. 1. 回答person的ret...
    smile麗語閱讀 1,747評論 0 7
  • 一般暴庫的方法和如何防止ACCESS數據庫被暴 編輯注釋:不好意思漂彤,其中還有一點亂碼,影響觀看 暴庫的方式有多種多...
    無聊DK閱讀 562評論 0 0
  • 網絡Socket 套接字Socket 編程寫數據到輸出流:PrintWriterSocket 模仿向服務端上傳文件...
    小石頭呢閱讀 873評論 0 4
  • 什么是Json JavaScript Object Notation灾搏,JavaScript的對象表示法挫望,是一種輕量...
    Jotyy閱讀 676評論 0 1
  • *面試心聲:其實這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個offer,總結起來就是把...
    Dove_iOS閱讀 27,160評論 30 470