Nodejs发送到Android Rar(图像文件)

时间:2022-05-11 09:00:41

I want to send image Node.js server to Android Clients.
I am using a REST Service between Node.js and Android Devices.
I can send image using node.js module 'fs' and receive Android device.
It's ok but i have over 200 images and each image's size between 1KB and 2KB. It' s very small images. So i dont want to send one by one. Its too slow so i am curious about if i ".rar" all image file (about 2MB), can i send one time and show images in android devices?
Or are there any way to send one time without ".rar" ?

我想发送图像节点。js服务器到Android客户端。我正在节点之间使用REST服务。js和Android设备。我可以使用节点发送图像。js模块“fs”,接收Android设备。可以,但是我有超过200张图片,每个图片的大小在1KB到2KB之间。这是非常小的图像。所以我不想一个一个地发送。它太慢了,所以我很好奇我是否。rar“所有的图像文件(约2MB),我可以一次发送并在android设备中显示图像吗?”或者有没有一种方式可以发送一次没有。rar”?

1 个解决方案

#1


0  

Of course you can compress them in an archive(any kind) and decompress them on the device.

当然,您可以将它们压缩到存档中(任何类型),并在设备上解压缩它们。

Using nodejs-zip you can generate zip archives. An example of compression (taken from here)

使用nodejs-zip可以生成zip档案。压缩的一个例子(从这里)

var http = require('http'),
nodejszip = require('../lib/nodejs-zip');
http.createServer(function (req, res) {
    var file = 'compress-example.zip',
    arguments = ['-j'],
    fileList = [
        'assets/image_1.jpg',
        'assets/image_2.jpg',
        'assets/image_3.jpg',
        'assets/image_4.jpg',
        'assets/image_5.jpg',
        'assets/image_6.jpg',
        'assets/image_7.jpg',
        'assets/image_8.jpg',
        'assets/image_9.jpg',
        'assets/image_10.jpg',
        'assets/image_11.jpg',
        'assets/image_12.jpg',
        'assets/image_13.jpg',
        'assets/image_14.jpg'];
    var zip = new nodejszip();
    zip.compress(file, fileList, arguments, function(err) {
        if (err) {
            throw err;
        }
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('Complete.\n');
    });
}).listen(8000);

On the device you can decompress a zip archive like that.(taken from here)

在设备上,您可以像这样解压zip归档文件。(从这里)

public class Decompress {  
  private String _zipFile; 
  private String _location; 

  public Decompress(String zipFile, String location) { 
    _zipFile = zipFile; 
    _location = location; 
    _dirChecker("");  
  } 



  public void unzip() { 
    try  { 
      FileInputStream fin = new FileInputStream(_zipFile); 
      ZipInputStream zin = new ZipInputStream(fin); 
      ZipEntry ze = null; 
      while ((ze = zin.getNextEntry()) != null) { 
        Log.v("Decompress", "Unzipping " + ze.getName()); 

        if(ze.isDirectory()) { 
          _dirChecker(ze.getName());    
        } else { 
          FileOutputStream fout = new FileOutputStream(_location + ze.getName());    
          for (int c = zin.read(); c != -1; c = zin.read()) { 
            fout.write(c);     
          } 
          zin.closeEntry(); 
          fout.close(); 
        } 
      }  
      zin.close(); 
    } catch(Exception e) {     
      Log.e("Decompress", "unzip", e); 
    } 
  } 

  private void _dirChecker(String dir) {    
    File f = new File(_location + dir);  
    if(!f.isDirectory()) { 
      f.mkdirs(); 
    } 
  } 
}

#1


0  

Of course you can compress them in an archive(any kind) and decompress them on the device.

当然,您可以将它们压缩到存档中(任何类型),并在设备上解压缩它们。

Using nodejs-zip you can generate zip archives. An example of compression (taken from here)

使用nodejs-zip可以生成zip档案。压缩的一个例子(从这里)

var http = require('http'),
nodejszip = require('../lib/nodejs-zip');
http.createServer(function (req, res) {
    var file = 'compress-example.zip',
    arguments = ['-j'],
    fileList = [
        'assets/image_1.jpg',
        'assets/image_2.jpg',
        'assets/image_3.jpg',
        'assets/image_4.jpg',
        'assets/image_5.jpg',
        'assets/image_6.jpg',
        'assets/image_7.jpg',
        'assets/image_8.jpg',
        'assets/image_9.jpg',
        'assets/image_10.jpg',
        'assets/image_11.jpg',
        'assets/image_12.jpg',
        'assets/image_13.jpg',
        'assets/image_14.jpg'];
    var zip = new nodejszip();
    zip.compress(file, fileList, arguments, function(err) {
        if (err) {
            throw err;
        }
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('Complete.\n');
    });
}).listen(8000);

On the device you can decompress a zip archive like that.(taken from here)

在设备上,您可以像这样解压zip归档文件。(从这里)

public class Decompress {  
  private String _zipFile; 
  private String _location; 

  public Decompress(String zipFile, String location) { 
    _zipFile = zipFile; 
    _location = location; 
    _dirChecker("");  
  } 



  public void unzip() { 
    try  { 
      FileInputStream fin = new FileInputStream(_zipFile); 
      ZipInputStream zin = new ZipInputStream(fin); 
      ZipEntry ze = null; 
      while ((ze = zin.getNextEntry()) != null) { 
        Log.v("Decompress", "Unzipping " + ze.getName()); 

        if(ze.isDirectory()) { 
          _dirChecker(ze.getName());    
        } else { 
          FileOutputStream fout = new FileOutputStream(_location + ze.getName());    
          for (int c = zin.read(); c != -1; c = zin.read()) { 
            fout.write(c);     
          } 
          zin.closeEntry(); 
          fout.close(); 
        } 
      }  
      zin.close(); 
    } catch(Exception e) {     
      Log.e("Decompress", "unzip", e); 
    } 
  } 

  private void _dirChecker(String dir) {    
    File f = new File(_location + dir);  
    if(!f.isDirectory()) { 
      f.mkdirs(); 
    } 
  } 
}