解压缩android上的文件似乎非常慢.起初我以为这只是模拟器,但在手机上似乎是一样的.我尝试过不同的压缩级别,并最终降级到存储模式,但仍需要很长时间.
无论如何,一定有理由!还有其他人有这个问题吗?我的解压缩方法如下所示:
public void unzip()
{
try{
        FileInputStream fin = new FileInputStream(zipFile);
        ZipInputStream zin = new ZipInputStream(fin);
        File rootfolder = new File(directory);
        rootfolder.mkdirs();
        ZipEntry ze = null;
        while ((ze = zin.getNextEntry())!=null){
            if(ze.isDirectory()){
                dirChecker(ze.getName());
            }
            else{
                FileOutputStream fout = new FileOutputStream(directory+ze.getName());
            for(int c = zin.read();c!=-1;c=zin.read()){
                fout.write(c);
            }
                //Debug.out("Closing streams");
                zin.closeEntry();
                fout.close();
        }
    }
    zin.close();
}
catch(Exception e){
            //Debug.out("Error trying to unzip file " + zipFile);
}
    }
如何在不使用课程的情况下获得InputStreama ?ZipEntryZipInputStreamZipFile
我试图从a读取一个文件java.util.zip.ZipInputStream,并将其复制到一个java.io.ByteArrayOutputStream(这样我就可以创建一个java.io.ByteArrayInputStream并将其交给第三方库,最终关闭流,我不希望我ZipInputStream关闭) .
我可能在这里遗漏了一些基本内容,但我从未在这里输入while循环:
ByteArrayOutputStream streamBuilder = new ByteArrayOutputStream();
int bytesRead;
byte[] tempBuffer = new byte[8192*2];
try {
    while ((bytesRead = zipStream.read(tempBuffer)) != -1) {
        streamBuilder.write(tempBuffer, 0, bytesRead);
    }
} catch (IOException e) {
    // ...
}
我错过了哪些可以让我复制流?
编辑:
我之前应该提到这ZipInputStream不是来自文件,所以我认为我不能使用ZipFile.它来自通过servlet上传的文件.
此外,我已经调用getNextEntry()了ZipInputStream之前获取此代码片段.如果我不尝试将文件复制到另一个文件中InputStream(通过OutputStream上面提到的),并且只是将文件传递ZipInputStream给我的第三方库,那么库将关闭流,而我无法做更多的事情,比如处理剩下的文件流.
我正在尝试读取一个zip文件,检查它是否有一些必需的文件,然后将所有有效文件写入另一个zip文件.java.util.zip的基本介绍有很多Java主义,我很想让我的代码更加Scala-native.具体来说,我想避免使用vars.这就是我所拥有的:
val fos = new FileOutputStream("new.zip");
val zipOut = new ZipOutputStream(new BufferedOutputStream(fos));
while (zipIn.available == 1) {
  val entry = zipIn.getNextEntry
  if (entryIsValid(entry)) {
    zipOut.putNewEntry(new ZipEntry("subdir/" + entry.getName())
    // read data into the data Array
    var data = Array[Byte](1024)
    var count = zipIn.read(data, 0, 1024)
    while (count != -1) {
      zipOut.write(data, 0, count)
      count = zipIn.read(data, 0, 1024)
    }
  }
  zipIn.close
}
zipOut.close
我应该补充说我正在使用Scala 2.7.7.
.zip文件中有多个文件,我正在尝试获取.尝试解压缩文件提供了java.lang.IllegalStateException:zis.nextEntry不能为null.怎么做正确的方法?
@Throws(IOException::class)
    fun unzip(zipFile: File, targetDirectory: File) {
        val zis = ZipInputStream(
                BufferedInputStream(FileInputStream(zipFile)))
        try {
            var ze: ZipEntry
            var count: Int
            val buffer = ByteArray(8192)
            ze = zis.nextEntry
            while (ze != null) {
                val file = File(targetDirectory, ze.name)
                val dir = if (ze.isDirectory) file else file.parentFile
                if (!dir.isDirectory && !dir.mkdirs())
                    throw FileNotFoundException("Failed to ensure directory: " + dir.absolutePath)
                if (ze.isDirectory)
                    continue
                val fout = FileOutputStream(file)
                try {
                    count = zis.read(buffer)
                    while (count != -1) {
                        fout.write(buffer, 0, count)
                        count = …在我的情况下,我必须从我的网络应用程序资源文件夹中下载图像.现在我使用以下代码通过URL下载图像.
url = new URL(properties.getOesServerURL() + "//resources//WebFiles//images//" + imgPath);
filename = url.getFile();               
is = url.openStream();
os = new FileOutputStream(sClientPhysicalPath + "//resources//WebFiles//images//" + imgPath);
b = new byte[2048];
while ((length = is.read(b)) != -1) {
    os.write(b, 0, length);
}
但是我想要一次操作来一次读取所有图像并为此创建一个zip文件.我不太了解序列输入流和zip输入流的使用,所以如果可以通过这些,请告诉我.
考虑将单个文件test_file.pdf放入zip存档test.zip然后读取此存档的代码示例:
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class Main {
    public static void main(String[] args) {
        File infile = new File("test_file.pdf");
        try (
                FileInputStream fis = new FileInputStream(infile);
                ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("test.zip"));
        ) {
            int bytesRead;
            byte[] buffer = new byte[1024];
            ZipEntry entry = new ZipEntry("data");
            entry.setSize(infile.length());
            zos.putNextEntry(entry);
            while ((bytesRead = fis.read(buffer)) >= 0)
            {
                zos.write(buffer, 0, bytesRead);
            }
            zos.closeEntry();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try (
                ZipInputStream zis …我可以通过ZipInputStream,但在开始迭代之前,我想获得迭代期间需要的特定文件.我怎样才能做到这一点?
ZipInputStream zin = new ZipInputStream(myInputStream)
while ((entry = zin.getNextEntry()) != null)
 {
    println entry.getName()
}
我试图解压缩String值.
但我得到一个java.io.IOException: Push back buffer is full:
public byte[] unzipArray(String stringToUnzip) {
            byte[] inputByteArray = Base64.decode(stringToUnzip);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                        inputByteArray);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ZipInputStream zipInputStream = new ZipInputStream(byteArrayInputStream);
            try {
                  ZipEntry theEntry = zipInputStream.getNextEntry();
            } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
            byte[] buffer = new byte[2048];
            int size = 2048;
            while (true) {
                  try {
                        size = zipInputStream.read(buffer, 0, buffer.length);
                  } catch (IOException e) {
                        // TODO Auto-generated catch …无论如何在我们完全读取流之前找到/估计ZipInputStream的大小?
例如,我们可以在读取userdata之前使用getNextEntry获取条目的元数据.
Inputstream有一个方法available()来返回可以从此输入流中读取的字节数的估计值,但我无法为ZipInputStream找到类似的方法.
zipinputstream ×10
java ×9
inputstream ×4
android ×2
zipfile ×2
buffer ×1
file-io ×1
groovy ×1
immutability ×1
ioexception ×1
java-8 ×1
kotlin ×1
scala ×1
spring-mvc ×1
unzip ×1
zip ×1